From 739c52fc84e004c92add3f7b72c27027028df4c3 Mon Sep 17 00:00:00 2001 From: velvettear Date: Fri, 18 Mar 2022 13:26:51 +0100 Subject: [PATCH] optimized api setup and handling --- config.json | 4 +-- libs/server.js | 81 +++++++++++++++++++++++++++----------------------- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/config.json b/config.json index c8e9838..f6bf160 100644 --- a/config.json +++ b/config.json @@ -10,7 +10,7 @@ "api": [ { "url": "/tail", - "type": "get", + "method": "get", "command": "tail", "args": [ "-f", "/tmp/test" @@ -19,7 +19,7 @@ }, { "url": "/uptime", - "type": "get", + "method": "get", "command": "uptime", "args": [ ] diff --git a/libs/server.js b/libs/server.js index 8e9565a..9b52d67 100644 --- a/libs/server.js +++ b/libs/server.js @@ -1,6 +1,7 @@ const logger = require('./logger.js'); const commands = require('./commands.js'); const http = require('http'); +const { config } = require('process'); let server; let api; @@ -15,7 +16,7 @@ async function start() { return new Promise((resolve, reject) => { server.listen(port, listen) .on('listening', function () { - logger.debug('server listening on ' + config.server.listen + ':' + config.server.port + '...'); + logger.info('server listening on ' + global.config.server.listen + ':' + global.config.server.port + '...'); handleRequests(); resolve(); }); @@ -25,46 +26,42 @@ async function start() { function handleRequests() { server.on('request', (request, response) => { request.timestamp = new Date().getTime(); - respond(request, response, api.get(request.url)); + respond(request, response); }); } -async function respond(request, response, endpoint) { +async function respond(request, response) { + if (request === undefined || response === undefined) { + return; + } + let endpoint = api[request.method]?.[request.url]; + if (endpoint === undefined) { + return finishRequest(request, response, { error: 'endpoint not defined' }, 501); + } + try { + endpoint.result = await commands.execute(endpoint); + } catch (err) { + endpoint.error = err.toString(); + return finishRequest(request, response, endpoint, 501); + } + return finishRequest(request, response, endpoint); +} + +function finishRequest(request, response, data, code) { if (response === undefined) { return; } - let data = { - status: 'ok', - command: endpoint.command, - args: endpoint.args - }; - if (endpoint.detached === true) { - data.detached = true; + if (code === undefined) { + code = 200; } - let code = 200; - if (endpoint === undefined) { - code = 501; - data.status = 'error'; - data.msg = 'endpoint not defined'; - } else if (request.method.toLowerCase() !== (endpoint.type.toLowerCase())) { - code = 405; - data.status = 'error'; - data.msg = 'endpoint does not support ' + request.method + ' requests'; + if (code === 200) { + data.status = 'ok'; } else { - try { - const result = await commands.execute(endpoint); - if (result !== undefined) { - data.result = result; - } - } catch (err) { - code = 501; - data.status = 'error'; - data.msg = err.toString(); - } + data.status = 'error'; } - data.time = new Date().getTime() - request.timestamp + 'ms'; - const json = JSON.stringify(data); + data.time = (new Date().getTime() - request.timestamp) + 'ms'; response.writeHead(code); + const json = JSON.stringify(data); response.end(json); logger.http({ request: request, code: code, data: json }); } @@ -74,13 +71,21 @@ function setupAPI() { if (apiConfig === undefined || apiConfig.length === 0) { throw new Error('no api endpoints configured - aborting'); } - api = new Map(); - config.api.forEach(function (endpoint) { - let url = endpoint.url; - let tmp = endpoint; - delete tmp.url; - api.set(url, tmp); - }); + const now = new Date().getTime(); + api = {}; + logger.debug('setting up api...'); + let index; + for (index = 0; index < global.config.api.length; index++) { + let endpoint = global.config.api[index]; + let key = endpoint.url; + let method = endpoint.method.toUpperCase(); + delete endpoint.method; + delete endpoint.url; + let methodApi = api[method] || {}; + methodApi[key] = endpoint; + api[method] = methodApi; + } + logger.debug('api setup with ' + index + ' endpoints after ' + (new Date().getTime() - now) + 'ms'); } module.exports = {