blinky/blinky.js

55 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-04-06 16:41:49 +02:00
// requirements
2022-02-21 00:48:29 +01:00
const server = require('./libs/server.js');
const util = require('./libs/util.js');
const logger = require('./libs/logger.js');
2021-04-06 16:41:49 +02:00
const packageJSON = require('./package');
2022-02-21 00:48:29 +01:00
const INTERRUPTS = ['SIGINT', 'SIGTERM'];
global.config = process.argv[2] || __dirname + '/config.json';
// handle interrupts
handleInterrupts();
2021-04-06 16:41:49 +02:00
// start the application
main();
// main - let's get this party started
async function main() {
try {
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();
} catch (err) {
exit(err);
}
2021-04-06 16:41:49 +02:00
}
// ... and it all comes crashing down
2022-02-21 00:48:29 +01:00
function exit(err, code) {
2022-02-21 11:56:49 +01:00
if (code === undefined) {
2022-02-21 00:48:29 +01:00
code = 0;
2022-02-21 11:56:49 +01:00
if (err !== undefined) {
2022-02-21 00:48:29 +01:00
code = 1;
}
}
2021-04-06 16:41:49 +02:00
if (err) {
logger.error(err);
logger.error(packageJSON.name + ' ended due to an error');
2021-04-06 16:41:49 +02:00
} else {
logger.info(packageJSON.name + ' shutting down gracefully')
2021-04-06 16:41:49 +02:00
}
process.exit(code);
}
2022-02-21 00:48:29 +01:00
function handleInterrupts() {
2022-02-21 11:56:49 +01:00
for (let index = 0; index < INTERRUPTS.length; index++) {
2022-02-21 00:48:29 +01:00
process.once(INTERRUPTS[index], (code) => {
exit(null, code);
});
}
}