// requirements const server = require('./libs/server.js'); const util = require('./libs/util.js'); const logger = require('./libs/logger.js'); const packageJSON = require('./package'); const INTERRUPTS = ['SIGINT', 'SIGTERM']; global.config = process.argv[2] || __dirname + '/config.json'; // handle interrupts handleInterrupts(); // start the application main(); // main - let's get this party started function main() { util.fileExists(global.config) .then((result) => { global.config = require(result.path); global.config.path = result.path; }) .then(logger.initialize) .then(server.start) .then(logger.info) .then(server.handleRequests) .catch(exit); } // ... and it all comes crashing down function exit(err, code) { if (code === undefined) { code = 0; if (err !== undefined) { code = 1; } } if (err) { logger.error(err); logger.error(packageJSON.name + " ended due to an error"); } else { logger.info(packageJSON.name + " shutting down gracefully") } process.exit(code); } function handleInterrupts() { for (let index = 0; index < INTERRUPTS.length; index++) { process.once(INTERRUPTS[index], (code) => { exit(null, code); }); } }