introduced global config variable and optimized application start
This commit is contained in:
parent
0ef17a5b70
commit
2b84ef5e97
4 changed files with 43 additions and 18 deletions
3
.vscode/launch.json
vendored
3
.vscode/launch.json
vendored
|
@ -8,7 +8,8 @@
|
||||||
"skipFiles": [
|
"skipFiles": [
|
||||||
"<node_internals>/**"
|
"<node_internals>/**"
|
||||||
],
|
],
|
||||||
"program": "${workspaceFolder}/ninwa.js"
|
"program": "${workspaceFolder}/ninwa.js",
|
||||||
|
"args": []
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
|
@ -1,4 +1,3 @@
|
||||||
const config = require('../config.json');
|
|
||||||
const moment = require('moment');
|
const moment = require('moment');
|
||||||
|
|
||||||
// constants
|
// constants
|
||||||
|
@ -11,12 +10,26 @@ const LOGLEVEL_INFO = 1;
|
||||||
const LOGLEVEL_WARNING = 2;
|
const LOGLEVEL_WARNING = 2;
|
||||||
const LOGLEVEL_ERROR = 3;
|
const LOGLEVEL_ERROR = 3;
|
||||||
|
|
||||||
const loglevel = getLogLevel();
|
var loglevel = getLogLevel();
|
||||||
const timestamp = getTimestamp();
|
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
|
// get the loglevel
|
||||||
function getLogLevel() {
|
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 LOG_PREFIX_DEBUG:
|
||||||
case LOGLEVEL_DEBUG:
|
case LOGLEVEL_DEBUG:
|
||||||
return LOGLEVEL_DEBUG;
|
return LOGLEVEL_DEBUG;
|
||||||
|
@ -36,8 +49,8 @@ function getLogLevel() {
|
||||||
|
|
||||||
// get the timestamp format
|
// get the timestamp format
|
||||||
function getTimestamp() {
|
function getTimestamp() {
|
||||||
if (config != undefined && config.log != undefined && config.log.format != undefined) {
|
if (global.config?.log?.format != undefined) {
|
||||||
return config.log.timestamp;
|
return global.config.log.timestamp;
|
||||||
}
|
}
|
||||||
return "DD.MM.YYYY HH:mm:ss:SS";
|
return "DD.MM.YYYY HH:mm:ss:SS";
|
||||||
}
|
}
|
||||||
|
@ -108,6 +121,7 @@ function trace(message, prefix) {
|
||||||
|
|
||||||
// exports
|
// exports
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
initialize,
|
||||||
info,
|
info,
|
||||||
warn,
|
warn,
|
||||||
debug,
|
debug,
|
||||||
|
|
|
@ -3,18 +3,17 @@ const Watcher = require('./watcher.js');
|
||||||
|
|
||||||
const watchers = [];
|
const watchers = [];
|
||||||
|
|
||||||
function initialize(config) {
|
function initialize() {
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
if (config == undefined || config.path == undefined) {
|
if (global.config == undefined) {
|
||||||
reject('no config defined');
|
reject('could not initialize watchers, no config defined');
|
||||||
}
|
}
|
||||||
config = require(config.path);
|
if (global.config.watchers == undefined || global.config.watchers.length == 0) {
|
||||||
if (config.watchers == undefined || config.watchers.length == 0) {
|
reject('no watchers in config \'' + global.config.path + '\' defined');
|
||||||
reject('no watchers in config defined');
|
|
||||||
}
|
}
|
||||||
var tmp = []
|
var tmp = []
|
||||||
for (var index = 0; index < config.watchers.length; index++) {
|
for (var index = 0; index < global.config.watchers.length; index++) {
|
||||||
tmp.push(new Watcher(config.watchers[index], watcherCallback));
|
tmp.push(new Watcher(global.config.watchers[index], watcherCallback));
|
||||||
}
|
}
|
||||||
Promise.all(tmp.map(check))
|
Promise.all(tmp.map(check))
|
||||||
.then(resolve)
|
.then(resolve)
|
||||||
|
|
17
ninwa.js
17
ninwa.js
|
@ -5,13 +5,24 @@ const packageJSON = require('./package.json');
|
||||||
|
|
||||||
const INTERRUPTS = ['SIGINT', 'SIGTERM'];
|
const INTERRUPTS = ['SIGINT', 'SIGTERM'];
|
||||||
|
|
||||||
logger.info(packageJSON.name + ' ' + packageJSON.version + ' starting...');
|
global.config = process.argv[2] || __dirname + '/config.json';
|
||||||
|
|
||||||
handleInterrupts();
|
handleInterrupts();
|
||||||
|
|
||||||
const config = process.argv[2] || __dirname + '/config.json';
|
|
||||||
|
|
||||||
util.fileExists(config)
|
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.initialize)
|
||||||
.then(watchers.start)
|
.then(watchers.start)
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
|
|
Loading…
Reference in a new issue