replaced some more promises with async/await

This commit is contained in:
Daniel Sommer 2022-02-24 10:41:50 +01:00
parent ad111f7709
commit d4b776333f
3 changed files with 29 additions and 28 deletions

View file

@ -15,17 +15,17 @@ handleInterrupts();
main(); main();
// main - let's get this party started // main - let's get this party started
function main() { async function main() {
util.fileExists(global.config) try {
.then((result) => { const config = await util.fileExists(global.config);
global.config = require(result.path); global.config = require(config.path);
global.config.path = result.path; global.config.path = config.path;
}) logger.initialize();
.then(logger.initialize) logger.info(await server.start());
.then(server.start) server.handleRequests();
.then(logger.info) } catch (err) {
.then(server.handleRequests) exit(err);
.catch(exit); }
} }
// ... and it all comes crashing down // ... and it all comes crashing down
@ -38,9 +38,9 @@ function exit(err, code) {
} }
if (err) { if (err) {
logger.error(err); logger.error(err);
logger.error(packageJSON.name + " ended due to an error"); logger.error(packageJSON.name + ' ended due to an error');
} else { } else {
logger.info(packageJSON.name + " shutting down gracefully") logger.info(packageJSON.name + ' shutting down gracefully')
} }
process.exit(code); process.exit(code);
} }

View file

@ -14,14 +14,11 @@ let loglevel = getLogLevel();
let timestamp = getTimestamp(); let timestamp = getTimestamp();
function initialize() { function initialize() {
return new Promise((resolve, reject) => { if (global.config === undefined) {
if (global.config === undefined) { throw new Error('could not initialize logger, config is undefined');
reject('could not initialize logger, config is undefined'); }
} loglevel = getLogLevel();
loglevel = getLogLevel(); timestamp = getTimestamp();
timestamp = getTimestamp();
resolve();
});
} }
// get the loglevel // get the loglevel
@ -104,7 +101,11 @@ function error(message) {
} }
return; return;
} }
trace(message.message, 'error'); if (message.message) {
trace(message.message, 'error');
return
}
trace(message, 'error');
} }
// default logging function // default logging function

View file

@ -23,19 +23,19 @@ const MODE_POWEROFF = require('./blinkstick.js').MODE_POWEROFF;
const html = require('./index.js').get(); const html = require('./index.js').get();
// run the express http server and handle gets/posts // run the express http server and handle gets/posts
function start() { async function start() {
return new Promise(function (resolve, reject) { return await new Promise(function (resolve, reject) {
app.listen(config.server.port, config.server.listen) app.listen(config.server.port, config.server.listen)
.on('listening', function () { .on('listening', function () {
return resolve('server listening on ' + config.server.listen + ':' + config.server.port + '...') resolve('server listening on ' + config.server.listen + ':' + config.server.port + '...')
}) })
.on('error', function (err) { .on('error', function (err) {
return reject('error starting server (' + err + ')'); reject('error starting server (' + err + ')');
}); });
}); });
} }
async function handleRequests() { function handleRequests() {
// GET html page // GET html page
app.get('*', (request, response) => { app.get('*', (request, response) => {
logger.http(request); logger.http(request);
@ -121,7 +121,7 @@ async function handleRequests() {
}) })
.catch((err) => { .catch((err) => {
response.status(500); response.status(500);
response.end({status: 'error', error: err}); response.end(JSON.stringify({status: 'error', error: err.message}));
}); });
}); });
} }