2022-02-21 00:48:29 +01:00
|
|
|
const logger = require('./libs/logger.js');
|
2022-02-25 15:41:11 +01:00
|
|
|
const util = require('./libs/util.js');
|
|
|
|
const cli = require('./libs/cli.js');
|
|
|
|
const server = require('./libs/server.js');
|
2022-02-25 22:58:47 +01:00
|
|
|
const blinkstick = require('./libs/blinkstick.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'];
|
|
|
|
|
2022-02-25 15:41:11 +01:00
|
|
|
global.config = undefined;
|
|
|
|
global.appName = packageJSON.name;
|
|
|
|
global.appVersion = packageJSON.version;
|
2022-02-21 00:48:29 +01:00
|
|
|
|
|
|
|
// handle interrupts
|
|
|
|
handleInterrupts();
|
|
|
|
|
2021-04-06 16:41:49 +02:00
|
|
|
// start the application
|
2022-02-24 10:50:31 +01:00
|
|
|
main()
|
2022-02-28 01:53:13 +01:00
|
|
|
.catch(util.exit);
|
2021-04-06 16:41:49 +02:00
|
|
|
|
2022-02-24 10:41:50 +01:00
|
|
|
async function main() {
|
2022-02-25 15:41:11 +01:00
|
|
|
await setGlobalConfig();
|
2022-02-24 10:50:31 +01:00
|
|
|
logger.initialize();
|
2022-02-25 15:41:11 +01:00
|
|
|
logger.info(global.appName + ' ' + global.appVersion + ' starting...');
|
|
|
|
if (await cli.handleArguments()) {
|
|
|
|
exit();
|
|
|
|
}
|
2022-02-25 22:58:47 +01:00
|
|
|
await blinkstick.findBlinkstick();
|
2022-02-24 10:50:31 +01:00
|
|
|
logger.info(await server.start());
|
|
|
|
server.handleRequests();
|
2021-04-06 16:41:49 +02:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:41:11 +01:00
|
|
|
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) => {
|
2022-02-28 01:53:13 +01:00
|
|
|
util.exit(undefined, code);
|
2022-02-25 15:41:11 +01:00
|
|
|
});
|
|
|
|
}
|
2022-02-28 01:53:13 +01:00
|
|
|
}
|