adejs/index.js

117 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-09-23 11:02:29 +02:00
const config = require('./settings/config.js');
require('app-module-path').addPath(config.BASE_DIR);
require('app-module-path').addPath(config.UTILS_DIR);
2017-09-23 11:39:42 +02:00
require('app-module-path').addPath(config.CONTROLLERS_DIR);
2017-09-23 11:02:29 +02:00
const express = require('express');
const pug = require('pug');
const http = require('http');
const path = require('path');
const log = require('log');
2017-09-23 14:53:56 +02:00
const model = require('model');
2017-09-24 00:38:12 +02:00
const repl = require('repl');
const User = require('auth/models');
2017-09-23 11:02:29 +02:00
2017-09-23 14:53:56 +02:00
// Load models
model.load(config.CONTROLLERS_DIR);
function startServer() {
2017-09-23 11:02:29 +02:00
let app = express();
let http = require('http').Server(app);
2017-09-23 18:20:56 +02:00
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
2017-09-23 11:02:29 +02:00
let session = require('cookie-session');
let cookieParser = require('cookie-parser');
app.set('view engine', 'pug');
app.set('trust proxy', 1);
app.use(cookieParser(config.SECRET_KEY));
2019-01-21 04:10:48 +01:00
app.use(session({
keys: [config.SECRET_KEY],
maxAge: 365 * 24 * 60 * 60 * 1000
}));
2017-09-23 11:02:29 +02:00
// Log request and time to answer
app.use(function(req, res, next) {
let start = Date.now();
res.on('finish', function() {
log.request(req, res, Date.now() - start);
});
res.locals.session = req.session;
2017-09-30 11:31:29 +02:00
if (req.session.user !== undefined) {
req.session.user = User.fromJSON(req.session.user);
}
2017-09-23 11:02:29 +02:00
next();
});
// Give app access to all urls
app.use(require('create-url'));
// Load controllers
2017-09-23 11:39:42 +02:00
require('controllers')(app, config.CONTROLLERS_DIR);
2017-09-23 11:02:29 +02:00
// Static files
app.use('/static', express.static(config.STATIC_DIR));
// When route not found, raise not found
app.use(function(req, res) {
res.status(404);
2017-09-23 11:02:29 +02:00
res.setHeader('Content-Type', 'text/html');
2017-09-23 18:20:56 +02:00
res.render(config.BASE_DIR + '/templates/404.pug', res.locals, function(err, result) {
2017-09-23 11:02:29 +02:00
if (err)
console.log(err);
res.send(result);
});
});
http.listen(config.PORT, config.LISTEN_ADDRESS, function() {
log.ready(
'Now listening ' + config.LISTEN_ADDRESS +
':' + config.PORT
);
});
// On sigint, stop the server
process.on('SIGINT', function() {
process.stdout.write('\r');
log.debug('Stopping server...');
process.exit(0);
});
}
2017-09-23 14:53:56 +02:00
const commands = {
runserver: startServer,
reinitdb: model.reinitialize,
2017-09-24 00:38:12 +02:00
shell: repl.start,
2017-09-23 14:53:56 +02:00
}
function showHelp() {
console.log("Commands available:");
for (let command in commands) {
console.log('\t' + command);
}
}
2017-09-23 11:02:29 +02:00
if (require.main === module) {
2017-09-23 14:53:56 +02:00
if (process.argv.length != 3) {
showHelp();
return;
}
let command = process.argv[2];
if (commands[command] === undefined) {
showHelp();
return;
}
commands[command]();
2017-09-23 11:02:29 +02:00
}