blinky/blinky.js

54 lines
No EOL
1.5 KiB
JavaScript

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');
const packageJSON = require('./package');
const constants = require('./libs/constants.js');
const INTERRUPTS = ['SIGINT', 'SIGTERM'];
global.config = undefined;
global.appName = packageJSON.name;
global.appVersion = packageJSON.version;
// handle interrupts
handleInterrupts();
// start the application
main()
.catch(util.exit);
async function main() {
await setGlobalConfig();
logger.initialize();
logger.info(global.appName + ' ' + global.appVersion + ' starting...');
if (await cli.handleArguments()) {
util.exit();
}
await controller.mapBlinkSticks();
logger.info(await server.start());
server.handleRequests();
}
async function setGlobalConfig() {
let config = __dirname + '/config.json';
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) => {
util.exit(undefined, code);
});
}
}