blinky/blinky.js

54 lines
1.5 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-03-03 21:18:07 +01:00
const constants = require('./libs/constants.js');
2021-04-06 16:41:49 +02:00
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()) {
2022-03-03 21:18:07 +01:00
util.exit();
}
controller.mapBlinkSticks();
logger.info(await server.start());
server.handleRequests();
2021-04-06 16:41:49 +02:00
}
async function setGlobalConfig() {
let config = __dirname + '/config.json';
2022-03-03 21:18:07 +01:00
let index = process.argv.indexOf(constants.ARG_CONFIG);
if (index === -1) {
index = process.argv.indexOf(constants.ARG_CONFIG_SHORT);
}
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
}