const logger = require('./libs/logger.js'); const api = require('./libs/api.js'); const server = require('./libs/server.js') const util = require('./libs/util.js'); const blinky = require('./libs/blinky.js'); const cache = require('./libs/cache.js'); const packageJSON = require('./package.json'); const INTERRUPTS = ['beforeExit', 'SIGINT', 'SIGTERM']; global.appName = packageJSON.name; global.appVersion = packageJSON.version; global.config = process.argv[2] || __dirname + '/config.json'; handleExit(); util.fileExists(config) .then((result) => { global.config = require(result.path); global.config.path = result.path; }) .then(logger.initialize) .then(blinky.initialize) .then(() => { logger.info("launching " + packageJSON.name + " " + packageJSON.version); }) .then(cache.fill) .then(api.setupEndpoints) .then(server.start) .catch((err) => { exit(err); }); function handleExit() { for (var index = 0; index < INTERRUPTS.length; index++) { process.on(INTERRUPTS[index], async (code) => { exit(undefined, code); }); } } async function exit(err, code) { await blinky.poweroff(); 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); }