From fa0eba88e11d954df7f538df824b10af181b36e2 Mon Sep 17 00:00:00 2001 From: velvettear Date: Mon, 7 Feb 2022 23:50:16 +0100 Subject: [PATCH] endpoints reworked --- libs/api.js | 39 +++++++++++-------- libs/modep.js | 77 ++++++++++++++++++++++-------------- libs/server.js | 104 ++++++++++++++++++++++++++----------------------- libs/util.js | 8 +++- 4 files changed, 132 insertions(+), 96 deletions(-) diff --git a/libs/api.js b/libs/api.js index 306a33d..888f697 100644 --- a/libs/api.js +++ b/libs/api.js @@ -22,36 +22,41 @@ function setEndpoints(url, data) { logger.debug('setting up ' + elements.length + ' endpoint(s) for path \'' + url + '\' took ' + util.timeDiff(startTime) + 'ms'); } -function setEndpoint(url, types, data) { - logger.debug('setting up \'' + types + '\' endpoint \'' + url + '\'...') - endpoints.set(url, { types: types, data: data }); +function setEndpoint(url, types, method, args) { + logger.debug('setting up \'' + types + '\' endpoint \'' + url + '\'...'); + endpoints.set(url, { types: types, method: method, args: args }); } function getEndpoints() { return endpoints; } +// TODO: IMPLEMENT POST ENDPOINTS function setupEndpoints() { return new Promise(function (resolve, reject) { modep.getBanks() - .then(function (data) { - setEndpoints(constants.API_BANKS, data); + .then(function (banks) { + setEndpoint(constants.API_BANKS, constants.HTTP_GET, modep.getBanks); + for (var index = 0; index < banks.length; index++) { + var id = banks[index].id; + setEndpoint(constants.API_BANKS + '/' + id, constants.HTTP_GET, modep.getBankById, id) + } }) .then(modep.getPedalboards) - .then(function (data) { - setEndpoints(constants.API_PEDALBOARDS, data); - }) - .then(modep.getDefaultPedalboard) - .then(function (data) { - setEndpoint(constants.API_PEDALBOARDS_DEFAULT, constants.HTTP_GET, data); - }) - .then(modep.getCurrentPedalboard) - .then(function (data) { - setEndpoint(constants.API_PEDALBOARDS_CURRENT, constants.HTTP_GET, data); + .then(function (pedalboards) { + setEndpoint(constants.API_PEDALBOARDS, constants.HTTP_GET, modep.getPedalboards); + for (var index = 0; index < pedalboards.length; index++) { + var id = pedalboards[index].id; + setEndpoint(constants.API_PEDALBOARDS + '/' + id, constants.HTTP_GET, modep.getPedalboardById, id) + } }) .then(modep.getCurrentPedals) - .then(function (data) { - setEndpoints(constants.API_PEDALS, data); + .then(function (pedals) { + setEndpoint(constants.API_PEDALS, constants.HTTP_GET, modep.getCurrentPedals); + for (var index = 0; index < pedals.length; index++) { + var id = pedals[index].id; + setEndpoint(constants.API_PEDALS + '/' + id, constants.HTTP_GET, modep.getCurrentPedalById, id) + } }) .then(resolve) .catch(reject); diff --git a/libs/modep.js b/libs/modep.js index aafe064..f3bcb62 100644 --- a/libs/modep.js +++ b/libs/modep.js @@ -23,7 +23,7 @@ function reset() { function getBanks() { return new Promise(function (resolve, reject) { - if (banks) { + if (banks != undefined) { return resolve(banks); } // FAKE DATA @@ -47,9 +47,25 @@ function getBanks() { }); } +function getBankById(bankId) { + return new Promise(function (resolve, reject) { + getBanks() + .then(function (banks) { + for (var index = 0; index < banks.length; index++) { + if (banks[index].id != bankId) { + continue; + } + return resolve(banks[index]); + } + return reject('error: could not find bank by id \'' + bankId + '\''); + }) + .catch(reject); + }); +} + function getPedalboards() { return new Promise(function (resolve, reject) { - if (pedalboards) { + if (pedalboards != undefined) { return resolve(pedalboards); } // FAKE DATA @@ -90,7 +106,7 @@ function getPedalboards() { function getDefaultPedalboard() { return new Promise(function (resolve, reject) { - if (defaultPedalboard) { + if (defaultPedalboard != undefined) { return resolve(defaultPedalboard); } getPedalboardByBundle(constants.PEDALBOARD_DEFAULT) @@ -101,7 +117,7 @@ function getDefaultPedalboard() { function getCurrentPedalboard() { return new Promise(function (resolve, reject) { - if (currentPedalboard && currentPedalboard.id) { + if (currentPedalboard != undefined && currentPedalboard.id != undefined) { return resolve(currentPedalboard); } // FAKE DATA @@ -116,13 +132,15 @@ function getCurrentPedalboard() { // PRODUCTION util.httpGET(config.modep.host, config.modep.port, '/pedalboard/current') .then(getPedalboardByBundle) - .then(resolve) + .then(function (pedalboard) { + currentPedalboard = pedalboard; + return resolve(currentPedalboard) + }) .catch(reject); }); } -function -getPedalboardById(pedalboardId) { +function getPedalboardById(pedalboardId) { return new Promise(function (resolve, reject) { getPedalboards() .then(function (pedalboards) { @@ -132,7 +150,7 @@ getPedalboardById(pedalboardId) { } return resolve(pedalboards[index]); } - return reject('error: could not find pedalboard by id \'' + controlId + '\''); + return reject('error: could not find pedalboard by id \'' + pedalboardId + '\''); }) .catch(reject); }); @@ -140,13 +158,14 @@ getPedalboardById(pedalboardId) { function getPedalboardByBundle(pedalboardBundle) { return new Promise(function (resolve, reject) { + if (pedalboardBundle == undefined) { + getDefaultPedalboard() + .then(resolve) + .catch(reject); + return; + } getPedalboards() .then(function (pedalboards) { - if (!pedalboardBundle) { - return getDefaultPedalboard() - .then(resolve) - .catch(reject); - } for (var index = 0; index < pedalboards.length; index++) { if (pedalboards[index].bundle != pedalboardBundle) { continue; @@ -324,28 +343,28 @@ function setPedalboard(pedalboard) { if (!pedalboard || !pedalboard.bundle) { return reject('error: no bundle set for pedalboard'); } - if (pedalboard.id == currentPedalboard.id) { - return resolve('pedalboard \'' + pedalboard.id + '\' is already active'); - } - logger.debug("DERP") - - reset() - .then(logger.debug("RESETTED")) - .catch(logger.error); - - - // reset() - // .then(function () { - // util.httpPOST(config.modep.host, config.modep.port, '/pedalboard/load_bundle/?bundlepath=' + pedalboard.bundle) - // }) - // .then(getCurrentPedalboard) - // .catch(reject); + getCurrentPedalboard() + .then(function (currentPedalboard) { + if (pedalboard.id == currentPedalboard.id) { + return resolve('pedalboard \'' + pedalboard.id + '\' is already active'); + } + }) + .then(reset) + .then(util.httpPOST(config.modep.host, config.modep.port, '/pedalboard/load_bundle/?bundlepath=' + pedalboard.bundle)) + .then(function () { + currentPedalboard = pedalboard + parseCurrentPedalboard(currentPedalboard) + }) + .then(resolve) + .catch(reject); }); } module.exports = { getBanks, + getBankById, getPedalboards, + getPedalboardById, getDefaultPedalboard, getCurrentPedalboard, getCurrentPedals, diff --git a/libs/server.js b/libs/server.js index 5da4616..430446d 100644 --- a/libs/server.js +++ b/libs/server.js @@ -22,6 +22,9 @@ function start() { function handleRequests() { server.on('request', function (request, response) { logger.request(request); + if (request.url.endsWith('/')) { + request.url = request.url.substring(0, request.url.length - 1); + } var endpoint = api.getEndpoints().get(request.url); if (!endpoint) { var msg = 'endpoint \'' + request.url + '\' not defined'; @@ -38,11 +41,33 @@ function handleRequests() { return; } if (request.method == constants.HTTP_GET) { - handleGET(response, endpoint); + handleGET(endpoint) + .then(function (result) { + response.writeHead(200); + response.end(result); + }) + .catch(function (err, code) { + if (!code) { + code = 500; + } + response.writeHead(code); + response.end(err); + }); return; } if (request.method == constants.HTTP_POST) { - handlePOST(request, response, endpoint); + handlePOST(request, response) + .then(function (result) { + response.writeHead(200); + response.end(result); + }) + .catch(function (err, code) { + if (!code) { + code = 500; + } + response.writeHead(code); + response.end(err); + }); return; } var msg = 'endpoint \'' + request.url + '\' does not have any handlers for ' + request.method + ' requests'; @@ -52,54 +77,37 @@ function handleRequests() { }); } -function handleGET(response, endpoint) { - var data = endpoint.data; - if (!data) { - response.writeHead(500); - response.end('error: could not get data for endpoint') - return; - } - response.writeHead(200); - response.end(JSON.stringify(data)); +function handleGET(endpoint) { + return new Promise(function (resolve, reject) { + if (!endpoint.method) { + return reject('error: no method defined for endpoint \'' + endpoint.url + '\''); + } + endpoint.method(endpoint.args) + .then(function (data) { + return resolve(JSON.stringify(data)) + }) + .catch(reject); + }); } -function handlePOST(request, response, endpoint) { - if (request.url.startsWith(constants.API_PEDALS)) { - handlePOSTPedals(request) - .then(function (msg) { - response.writeHead(200); - response.end(msg); - }) - .catch(function (err, code) { - if (!code) { - code = 500; - } - response.writeHead(code); - response.end(err); - logger.error(err); - }); - return; - } - if (request.url.startsWith(constants.API_PEDALBOARDS)) { - handlePOSTPedalboards(request) - .then(function (msg) { - response.writeHead(200); - response.end(msg); - }) - .catch(function (err, code) { - if (!code) { - code = 500; - } - response.writeHead(code); - response.end(err); - logger.error(err); - }); - return; - } - var msg = 'endpoint \'' + request.url + '\' is not yet implemented for ' + request.method + ' requests'; - response.writeHead(405); - response.end(msg); - +function handlePOST(request, response) { + return new Promise(function (resolve, reject) { + if (request.url.startsWith(constants.API_PEDALS)) { + handlePOSTPedals(request) + .then(resolve) + .catch(reject); + return; + } + if (request.url.startsWith(constants.API_PEDALBOARDS)) { + handlePOSTPedalboards(request) + .then(resolve) + .catch(reject); + return; + } + var msg = 'endpoint \'' + request.url + '\' is not yet implemented for ' + request.method + ' requests'; + response.writeHead(405); + response.end(msg); + }); } function getPOSTParams(request) { diff --git a/libs/util.js b/libs/util.js index 8d47414..24726c8 100644 --- a/libs/util.js +++ b/libs/util.js @@ -1,6 +1,6 @@ const logger = require('./logger.js'); const http = require('http'); -const { HTTP_GET } = require('./constants.js'); +const { HTTP_GET, HTTP_POST } = require('./constants.js'); function timeDiff(startTime) { if (startTime instanceof Date) { @@ -50,7 +50,11 @@ function httpRequest(host, port, path, method, args) { if (!responseData) { return resolve(); } - return resolve(JSON.parse(responseData)); + try { + return resolve(JSON.parse(responseData)); + } catch (err) { + return resolve(responseData); + } }); }); request.on('error', function (err) {