2022-02-07 15:41:27 +01:00
|
|
|
const logger = require('./libs/logger.js');
|
|
|
|
const api = require('./libs/api.js');
|
|
|
|
const server = require('./libs/server.js')
|
2022-02-20 22:04:32 +01:00
|
|
|
const util = require('./libs/util.js');
|
2022-03-04 02:38:52 +01:00
|
|
|
const blinky = require('./libs/blinky.js');
|
2022-03-10 00:32:38 +01:00
|
|
|
const cache = require('./libs/cache.js');
|
2022-02-07 15:41:27 +01:00
|
|
|
const packageJSON = require('./package.json');
|
|
|
|
|
2022-03-08 00:51:58 +01:00
|
|
|
const INTERRUPTS = ['beforeExit', 'SIGINT', 'SIGTERM'];
|
2022-02-20 22:04:32 +01:00
|
|
|
|
2022-03-04 02:38:52 +01:00
|
|
|
global.appName = packageJSON.name;
|
|
|
|
global.appVersion = packageJSON.version;
|
2022-02-20 22:04:32 +01:00
|
|
|
global.config = process.argv[2] || __dirname + '/config.json';
|
|
|
|
|
2022-03-08 00:51:58 +01:00
|
|
|
handleExit();
|
2022-02-20 22:04:32 +01:00
|
|
|
|
|
|
|
util.fileExists(config)
|
|
|
|
.then((result) => {
|
|
|
|
global.config = require(result.path);
|
|
|
|
global.config.path = result.path;
|
|
|
|
})
|
|
|
|
.then(logger.initialize)
|
2022-03-04 02:38:52 +01:00
|
|
|
.then(blinky.initialize)
|
2022-02-20 22:04:32 +01:00
|
|
|
.then(() => {
|
|
|
|
logger.info("launching " + packageJSON.name + " " + packageJSON.version);
|
|
|
|
})
|
2022-03-10 00:32:38 +01:00
|
|
|
.then(cache.fill)
|
2022-02-20 22:04:32 +01:00
|
|
|
.then(api.setupEndpoints)
|
2022-02-07 15:41:27 +01:00
|
|
|
.then(server.start)
|
2022-02-20 22:04:32 +01:00
|
|
|
.catch((err) => {
|
2022-03-08 00:51:58 +01:00
|
|
|
exit(err);
|
2022-02-20 22:04:32 +01:00
|
|
|
});
|
|
|
|
|
2022-03-08 00:51:58 +01:00
|
|
|
function handleExit() {
|
2022-02-20 22:04:32 +01:00
|
|
|
for (var index = 0; index < INTERRUPTS.length; index++) {
|
2022-03-08 00:51:58 +01:00
|
|
|
process.on(INTERRUPTS[index], async (code) => {
|
|
|
|
exit(undefined, code);
|
2022-02-20 22:04:32 +01:00
|
|
|
});
|
|
|
|
}
|
2022-03-08 00:51:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function exit(err, code) {
|
|
|
|
await blinky.setActive(false);
|
|
|
|
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);
|
2022-02-20 22:04:32 +01:00
|
|
|
}
|