From 2139b0b1110e7b63366cff0fd5dbed6ee07ae589 Mon Sep 17 00:00:00 2001 From: velvettear Date: Fri, 25 Feb 2022 15:41:11 +0100 Subject: [PATCH] moved config initialization into own function --- blinky.js | 59 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/blinky.js b/blinky.js index d434399..6e6d676 100644 --- a/blinky.js +++ b/blinky.js @@ -1,12 +1,14 @@ -// requirements -const server = require('./libs/server.js'); -const util = require('./libs/util.js'); 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 packageJSON = require('./package'); const INTERRUPTS = ['SIGINT', 'SIGTERM']; -global.config = process.argv[2] || __dirname + '/config.json'; +global.config = undefined; +global.appName = packageJSON.name; +global.appVersion = packageJSON.version; // handle interrupts handleInterrupts(); @@ -15,31 +17,26 @@ handleInterrupts(); main() .catch(exit); -// main - let's get this party started async function main() { - const config = await util.fileExists(global.config); - global.config = require(config.path); - global.config.path = config.path; + await setGlobalConfig(); logger.initialize(); + logger.info(global.appName + ' ' + global.appVersion + ' starting...'); + if (await cli.handleArguments()) { + exit(); + } logger.info(await server.start()); server.handleRequests(); } -// ... and it all comes crashing down -function exit(err, code) { - if (code === undefined) { - code = 0; - if (err !== undefined) { - code = 1; - } +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]; } - if (err) { - logger.error(err); - logger.error(packageJSON.name + ' ended due to an error'); - } else { - logger.info(packageJSON.name + ' shutting down gracefully') - } - process.exit(code); + config = await util.fileExists(config); + global.config = require(config.path); + global.config.path = config.path; } function handleInterrupts() { @@ -49,3 +46,21 @@ function handleInterrupts() { }); } } + +function exit(err, code) { + 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); +} + +