2014-05-12 20:08:19 +02:00
|
|
|
'use strict';
|
|
|
|
|
2014-06-06 21:22:57 +02:00
|
|
|
angular.module('ffffng').factory('app', function (fs, config, _) {
|
2014-05-12 20:08:19 +02:00
|
|
|
var express = require('express');
|
2016-05-24 19:40:02 +02:00
|
|
|
var auth = require('http-auth');
|
2014-06-06 14:57:20 +02:00
|
|
|
var bodyParser = require('body-parser');
|
|
|
|
var compress = require('compression');
|
|
|
|
|
2014-05-12 20:08:19 +02:00
|
|
|
var app = express();
|
|
|
|
|
2016-05-24 19:40:02 +02:00
|
|
|
// urls beneath /internal are protected
|
|
|
|
var internalAuth = auth.basic(
|
|
|
|
{
|
|
|
|
realm: "Knotenformular - Intern"
|
|
|
|
},
|
|
|
|
function (username, password, callback) {
|
|
|
|
callback(
|
|
|
|
config.server.internal.active &&
|
|
|
|
username === config.server.internal.user &&
|
|
|
|
password === config.server.internal.password
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
app.use('/internal', auth.connect(internalAuth));
|
|
|
|
|
2014-08-10 11:54:25 +02:00
|
|
|
app.use(bodyParser.json());
|
2016-06-07 14:08:04 +02:00
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2016-06-04 14:58:11 +02:00
|
|
|
var adminDir = __dirname + '/../admin';
|
2014-05-12 20:08:19 +02:00
|
|
|
var clientDir = __dirname + '/../client';
|
2014-06-06 21:22:57 +02:00
|
|
|
var templateDir = __dirname + '/templates';
|
|
|
|
|
|
|
|
var jsTemplateFiles = [
|
|
|
|
'/config.js'
|
|
|
|
];
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2014-06-06 14:57:20 +02:00
|
|
|
app.use(compress());
|
2014-06-06 21:22:57 +02:00
|
|
|
|
|
|
|
function serveTemplate(mimeType, req, res, next) {
|
|
|
|
return fs.readFile(templateDir + '/' + req.path, 'utf8', function (err, body) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.writeHead(200, {'Content-Type': mimeType});
|
2016-05-16 13:33:49 +02:00
|
|
|
res.end(_.template(body)( { config: config.client }));
|
2014-06-06 21:22:57 +02:00
|
|
|
|
|
|
|
return null; // to suppress warning
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-24 16:40:57 +02:00
|
|
|
app.use(function (req, res, next) {
|
2014-06-06 21:22:57 +02:00
|
|
|
if (jsTemplateFiles.indexOf(req.path) >= 0) {
|
|
|
|
return serveTemplate('application/javascript', req, res, next);
|
|
|
|
}
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2016-06-04 14:58:11 +02:00
|
|
|
app.use('/internal/admin', express.static(adminDir + '/'));
|
2014-05-12 20:08:19 +02:00
|
|
|
app.use('/', express.static(clientDir + '/'));
|
|
|
|
|
|
|
|
return app;
|
|
|
|
});
|