const logger = require('./libs/logger.js'); const util = require('./libs/util.js'); const cli = require('./libs/cli.js'); const server = require('./libs/server.js'); const packageJSON = require('./package'); const INTERRUPTS = ['SIGINT', 'SIGTERM']; global.config = undefined; global.appName = packageJSON.name; global.appVersion = packageJSON.version; // handle interrupts handleInterrupts(); // start the application main() .catch(exit); async function main() { await setGlobalConfig(); logger.initialize(); logger.info(global.appName + ' ' + global.appVersion + ' starting...'); if (await cli.handleArguments()) { exit(); } logger.info(await server.start()); server.handleRequests(); } async function setGlobalConfig() { let config = __dirname + '/config.json'; let index = process.argv.indexOf(cli.ARG_CONFIG); if (index >= 0 && process.argv.length >= index + 1) { config = process.argv[index + 1]; } config = await util.fileExists(config); global.config = require(config.path); global.config.path = config.path; } function handleInterrupts() { for (let index = 0; index < INTERRUPTS.length; index++) { process.once(INTERRUPTS[index], (code) => { exit(null, code); }); } } function exit(err, code) { if (code === undefined) { code = 0; if (err !== undefined) { code = 1; } } if (err) { logger.error(err); logger.error(global.appName + ' ' + global.appVersion + ' ended due to an error'); } else { logger.info(global.appName + ' ' + global.appVersion + ' shutting down gracefully') } process.exit(code); }