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
function main() {
2022-02-21 00:48:29 +01:00
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);
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");
} else {
logger.info(packageJSON.name + " shutting down gracefully")
}
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);
});
}
}