ffffng/server/app.js

67 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

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();
var router = express.Router();
2014-05-12 20:08:19 +02:00
2016-05-24 19:40:02 +02:00
// urls beneath /internal are protected
var internalAuth = auth.basic(
{
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
);
}
);
router.use('/internal', auth.connect(internalAuth));
2016-05-24 19:40:02 +02:00
router.use(bodyParser.json());
router.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
router.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
});
}
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();
});
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;
});