mailtrain/index.js
2018-04-29 18:13:40 +02:00

114 lines
3.9 KiB
JavaScript

'use strict';
const config = require('config');
const log = require('npmlog');
const appBuilder = require('./app-builder');
const http = require('http');
//const triggers = require('./services/triggers');
// const importer = require('./services/importer');
// const verpServer = require('./services/verp-server');
const testServer = require('./services/test-server');
//const postfixBounceServer = require('./services/postfix-bounce-server');
const tzupdate = require('./services/tzupdate');
//const feedcheck = require('./services/feedcheck');
const dbcheck = require('./lib/dbcheck');
//const senders = require('./lib/senders');
const reportProcessor = require('./lib/report-processor');
const executor = require('./lib/executor');
const privilegeHelpers = require('./lib/privilege-helpers');
const knex = require('./lib/knex');
const shares = require('./models/shares');
const trustedPort = config.www.port;
const sandboxPort = config.www.sandboxPort;
const host = config.www.host;
if (config.title) {
process.title = config.title;
}
log.level = config.log.level;
function startHTTPServer(trusted, port, callback) {
const app = appBuilder.createApp(trusted);
app.set('port', port);
const server = http.createServer(app);
server.on('error', err => {
if (err.syscall !== 'listen') {
throw err;
}
const bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port;
// handle specific listen errors with friendly messages
switch (err.code) {
case 'EACCES':
log.error('Express', '%s requires elevated privileges', bind);
return process.exit(1);
case 'EADDRINUSE':
log.error('Express', '%s is already in use', bind);
return process.exit(1);
default:
throw err;
}
});
server.on('listening', () => {
const addr = server.address();
const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
log.info('Express', 'WWW server listening on %s', bind);
});
server.listen({port, host}, callback);
}
// ---------------------------------------------------------------------------------------
// Start the whole circus here
// ---------------------------------------------------------------------------------------
dbcheck(err => { // Check if database needs upgrading before starting the server - legacy migration first
if (err) {
log.error('DB', err.message || err);
return process.exit(1);
}
knex.migrate.latest() // And now the current migration with Knex
.then(() => shares.regenerateRoleNamesTable())
.then(() => shares.rebuildPermissions())
.then(() =>
executor.spawn(() => {
testServer(() => {
//verpServer(() => {
startHTTPServer(true, trustedPort, () => {
startHTTPServer(false, sandboxPort, () => {
privilegeHelpers.dropRootPrivileges();
tzupdate.start();
//importer(() => {
//triggers(() => {
//senders.spawn(() => {
//feedcheck(() => {
//postfixBounceServer(async () => {
(async () => {
await reportProcessor.init();
log.info('Service', 'All services started');
})();
//});
//});
//});
//});
//});
});
});
//});
});
})
);
});