modified some config stuff

This commit is contained in:
Daniel Sommer 2022-02-20 22:04:32 +01:00
parent 505400d1f4
commit 7e0dd27b78
5 changed files with 97 additions and 15 deletions

View file

@ -1,15 +1,15 @@
{ {
"server": { "server": {
"listen": "0.0.0.0", "listen": "0.0.0.0",
"port": 3000, "port": 3000
"timestamp": "DD.MM.YYYY HH:mm:ss:SS"
}, },
"cache": { "cache": {
"active": false, "active": false,
"lifetime": 15 "lifetime": 15
}, },
"log": { "log": {
"level": "debug" "level": "debug",
"format": "DD.MM.YYYY HH:mm:ss:SS"
}, },
"osc": { "osc": {
"host": "192.168.1.24", "host": "192.168.1.24",

View file

@ -1,4 +1,3 @@
const logger = require('./logger.js');
const cache = require('./cache.js'); const cache = require('./cache.js');
const commands = require('./commands.js'); const commands = require('./commands.js');
const os = require('os'); const os = require('os');

View file

@ -1,4 +1,3 @@
const config = require('../config.json');
const moment = require('moment'); const moment = require('moment');
// constants // constants
@ -11,9 +10,26 @@ const LOGLEVEL_INFO = 1;
const LOGLEVEL_WARNING = 2; const LOGLEVEL_WARNING = 2;
const LOGLEVEL_ERROR = 3; const LOGLEVEL_ERROR = 3;
// set loglevel on 'require' var loglevel = getLogLevel();
const loglevel = function () { var timestamp = getTimestamp();
switch (config.log.level) {
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() {
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;
@ -29,7 +45,15 @@ const loglevel = function () {
default: default:
return LOGLEVEL_INFO; return LOGLEVEL_INFO;
} }
}(); }
// get the timestamp format
function getTimestamp() {
if (global.config?.log?.format != undefined) {
return global.config.log.format;
}
return "DD.MM.YYYY HH:mm:ss:SS";
}
// log a http request - response object // log a http request - response object
function http(object) { function http(object) {
@ -109,12 +133,13 @@ function trace(message, prefix) {
default: default:
print = console.log; print = console.log;
} }
message = moment().format(config.server.timestamp) + ' | ' + prefix + ' > ' + message; message = moment().format(timestamp) + ' | ' + prefix + ' > ' + message;
print(message); print(message);
} }
// exports // exports
module.exports = { module.exports = {
initialize,
info, info,
warn, warn,
debug, debug,

View file

@ -1,4 +1,6 @@
const logger = require('./logger.js'); const logger = require('./logger.js');
const realpath = require('fs/promises').realpath;
const stat = require('fs/promises').stat;
const http = require('http'); const http = require('http');
const { HTTP_GET, HTTP_POST } = require('./constants.js'); const { HTTP_GET, HTTP_POST } = require('./constants.js');
@ -79,11 +81,42 @@ function sortById(array) {
}); });
} }
function fileExists(file) {
return new Promise((resolve, reject) => {
if (file == undefined) {
reject('can not check the existence of an undefined file');
}
resolvePath(file)
.then((path) => {
stat(path)
.then((stats) => {
resolve({path, stats});
})
})
.catch(reject);
});
}
function resolvePath(file) {
return new Promise((resolve, reject) => {
if (file == undefined) {
reject('can not resolve a path to an undefined file');
}
realpath(file)
.then(resolve)
.catch((err) => {
reject('resolving path \'' + file + '\' encountered an error >>> ' + err);
});
});
}
module.exports = { module.exports = {
timeDiff, timeDiff,
clone, clone,
httpGET, httpGET,
httpPOST, httpPOST,
toHex, toHex,
sortById sortById,
fileExists,
resolvePath
} }

31
pbc.js
View file

@ -1,12 +1,37 @@
const logger = require('./libs/logger.js'); const logger = require('./libs/logger.js');
const api = require('./libs/api.js'); const api = require('./libs/api.js');
const server = require('./libs/server.js') const server = require('./libs/server.js')
const util = require('./libs/util.js');
const packageJSON = require('./package.json'); const packageJSON = require('./package.json');
logger.info("launching " + packageJSON.name + " " + packageJSON.version); const INTERRUPTS = ['SIGINT', 'SIGTERM'];
api.setupEndpoints()
global.config = process.argv[2] || __dirname + '/config.json';
handleInterrupts();
util.fileExists(config)
.then((result) => {
global.config = require(result.path);
global.config.path = result.path;
})
.then(logger.initialize)
.then(() => {
logger.info("launching " + packageJSON.name + " " + packageJSON.version);
})
.then(api.setupEndpoints)
.then(server.start) .then(server.start)
.catch(function (err) { .catch((err) => {
logger.error(err); logger.error(err);
process.exit(1); process.exit(1);
}); });
function handleInterrupts() {
for (var index = 0; index < INTERRUPTS.length; index++) {
process.once(INTERRUPTS[index], (code) => {
watchers.stop()
.then(exit(code))
.catch(exit(code));
});
}
}