moved config initialization into own function

This commit is contained in:
Daniel Sommer 2022-02-25 15:41:11 +01:00
parent 4d2dcbf4f0
commit 2139b0b111

View file

@ -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);
}