2014-05-12 20:08:19 +02:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
const _ = require('lodash')
|
|
|
|
const auth = require('http-auth');
|
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
const compress = require('compression');
|
|
|
|
const express = require('express');
|
|
|
|
const fs = require('graceful-fs')
|
2014-06-06 14:57:20 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
const config = require('./config').config
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
|
|
|
module.exports = (() => {
|
|
|
|
const router = express.Router();
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2016-05-24 19:40:02 +02:00
|
|
|
// urls beneath /internal are protected
|
2018-12-17 22:49:54 +01:00
|
|
|
const internalAuth = auth.basic(
|
2016-05-24 19:40:02 +02:00
|
|
|
{
|
2017-05-06 17:31:34 +02:00
|
|
|
realm: 'Knotenformular - Intern'
|
2016-05-24 19:40:02 +02:00
|
|
|
},
|
|
|
|
function (username, password, callback) {
|
|
|
|
callback(
|
|
|
|
config.server.internal.active &&
|
|
|
|
username === config.server.internal.user &&
|
|
|
|
password === config.server.internal.password
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2017-05-06 18:40:59 +02:00
|
|
|
router.use('/internal', auth.connect(internalAuth));
|
2016-05-24 19:40:02 +02:00
|
|
|
|
2017-05-06 18:40:59 +02:00
|
|
|
router.use(bodyParser.json());
|
|
|
|
router.use(bodyParser.urlencoded({ extended: true }));
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
const adminDir = __dirname + '/../admin';
|
|
|
|
const clientDir = __dirname + '/../client';
|
|
|
|
const templateDir = __dirname + '/templates';
|
2014-06-06 21:22:57 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
const jsTemplateFiles = [
|
2014-06-06 21:22:57 +02:00
|
|
|
'/config.js'
|
|
|
|
];
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2017-05-06 18:40:59 +02:00
|
|
|
router.use(compress());
|
2014-06-06 21:22:57 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
function serveTemplate (mimeType, req, res, next) {
|
2014-06-06 21:22:57 +02:00
|
|
|
return fs.readFile(templateDir + '/' + req.path, 'utf8', function (err, body) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
res.writeHead(200, { 'Content-Type': mimeType });
|
|
|
|
res.end(_.template(body)({ config: config.client }));
|
2014-06-06 21:22:57 +02:00
|
|
|
|
|
|
|
return null; // to suppress warning
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-06 18:40:59 +02:00
|
|
|
router.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();
|
|
|
|
});
|
|
|
|
|
2017-05-06 18:40:59 +02:00
|
|
|
router.use('/internal/admin', express.static(adminDir + '/'));
|
|
|
|
router.use('/', express.static(clientDir + '/'));
|
|
|
|
|
|
|
|
app.use(config.server.rootPath, router);
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
return app;
|
2018-12-17 22:49:54 +01:00
|
|
|
})()
|