introduced global config variable and optimized application start

This commit is contained in:
Daniel Sommer 2022-02-20 21:38:27 +01:00
parent 0ef17a5b70
commit 2b84ef5e97
4 changed files with 43 additions and 18 deletions

3
.vscode/launch.json vendored
View file

@ -8,7 +8,8 @@
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/ninwa.js"
"program": "${workspaceFolder}/ninwa.js",
"args": []
}
]
}

View file

@ -1,4 +1,3 @@
const config = require('../config.json');
const moment = require('moment');
// constants
@ -11,12 +10,26 @@ const LOGLEVEL_INFO = 1;
const LOGLEVEL_WARNING = 2;
const LOGLEVEL_ERROR = 3;
const loglevel = getLogLevel();
const timestamp = getTimestamp();
var loglevel = getLogLevel();
var timestamp = getTimestamp();
function initialize() {
return new Promise((resolve, reject) => {
if (global.config == undefined) {
reject('could not initialize logger, config is undefined');
}
loglevel = getLogLevel();
timestamp = getTimestamp();
resolve();
});
}
// get the loglevel
function getLogLevel() {
switch (config.log.level) {
if (global.config?.log?.level == undefined) {
return LOGLEVEL_INFO;
}
switch (global.config.log.level) {
case LOG_PREFIX_DEBUG:
case LOGLEVEL_DEBUG:
return LOGLEVEL_DEBUG;
@ -36,8 +49,8 @@ function getLogLevel() {
// get the timestamp format
function getTimestamp() {
if (config != undefined && config.log != undefined && config.log.format != undefined) {
return config.log.timestamp;
if (global.config?.log?.format != undefined) {
return global.config.log.timestamp;
}
return "DD.MM.YYYY HH:mm:ss:SS";
}
@ -108,6 +121,7 @@ function trace(message, prefix) {
// exports
module.exports = {
initialize,
info,
warn,
debug,

View file

@ -3,18 +3,17 @@ const Watcher = require('./watcher.js');
const watchers = [];
function initialize(config) {
function initialize() {
return new Promise(function (resolve, reject) {
if (config == undefined || config.path == undefined) {
reject('no config defined');
if (global.config == undefined) {
reject('could not initialize watchers, no config defined');
}
config = require(config.path);
if (config.watchers == undefined || config.watchers.length == 0) {
reject('no watchers in config defined');
if (global.config.watchers == undefined || global.config.watchers.length == 0) {
reject('no watchers in config \'' + global.config.path + '\' defined');
}
var tmp = []
for (var index = 0; index < config.watchers.length; index++) {
tmp.push(new Watcher(config.watchers[index], watcherCallback));
for (var index = 0; index < global.config.watchers.length; index++) {
tmp.push(new Watcher(global.config.watchers[index], watcherCallback));
}
Promise.all(tmp.map(check))
.then(resolve)

View file

@ -5,13 +5,24 @@ const packageJSON = require('./package.json');
const INTERRUPTS = ['SIGINT', 'SIGTERM'];
logger.info(packageJSON.name + ' ' + packageJSON.version + ' starting...');
global.config = process.argv[2] || __dirname + '/config.json';
handleInterrupts();
const config = process.argv[2] || __dirname + '/config.json';
util.fileExists(config)
.catch((err) => {
logger.error('given config file \'' + config + '\' does not exist');
logger.error(err);
exit(1);
})
.then((result) => {
global.config = require(result.path);
global.config.path = result.path;
})
.then(logger.initialize)
.then(() => {
logger.info(packageJSON.name + ' ' + packageJSON.version + ' starting...');
})
.then(watchers.initialize)
.then(watchers.start)
.catch((err) => {