blinky/blinky.js

50 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-02-21 00:48:29 +01:00
const logger = require('./libs/logger.js');
const util = require('./libs/util.js');
const cli = require('./libs/cli.js');
const server = require('./libs/server.js');
const controller = require('./libs/controller.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 = 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
main()
2022-02-28 01:53:13 +01:00
.catch(util.exit);
2021-04-06 16:41:49 +02:00
async function main() {
await setGlobalConfig();
logger.initialize();
logger.info(global.appName + ' ' + global.appVersion + ' starting...');
if (await cli.handleArguments()) {
exit();
}
await controller.findBlinkstick();
logger.info(await server.start());
server.handleRequests();
2021-04-06 16:41:49 +02: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-28 01:53:13 +01:00
}