blinky/blinky.js

51 lines
1.3 KiB
JavaScript

// 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()
.catch(exit);
// main - let's get this party started
async function main() {
const config = await util.fileExists(global.config);
global.config = require(config.path);
global.config.path = config.path;
logger.initialize();
logger.info(await server.start());
server.handleRequests();
}
// ... 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);
});
}
}