endpoints reworked

This commit is contained in:
Daniel Sommer 2022-02-07 23:50:16 +01:00
parent e7fb5dc68c
commit fa0eba88e1
4 changed files with 132 additions and 96 deletions

View file

@ -22,36 +22,41 @@ function setEndpoints(url, data) {
logger.debug('setting up ' + elements.length + ' endpoint(s) for path \'' + url + '\' took ' + util.timeDiff(startTime) + 'ms'); logger.debug('setting up ' + elements.length + ' endpoint(s) for path \'' + url + '\' took ' + util.timeDiff(startTime) + 'ms');
} }
function setEndpoint(url, types, data) { function setEndpoint(url, types, method, args) {
logger.debug('setting up \'' + types + '\' endpoint \'' + url + '\'...') logger.debug('setting up \'' + types + '\' endpoint \'' + url + '\'...');
endpoints.set(url, { types: types, data: data }); endpoints.set(url, { types: types, method: method, args: args });
} }
function getEndpoints() { function getEndpoints() {
return endpoints; return endpoints;
} }
// TODO: IMPLEMENT POST ENDPOINTS
function setupEndpoints() { function setupEndpoints() {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
modep.getBanks() modep.getBanks()
.then(function (data) { .then(function (banks) {
setEndpoints(constants.API_BANKS, data); 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(modep.getPedalboards)
.then(function (data) { .then(function (pedalboards) {
setEndpoints(constants.API_PEDALBOARDS, data); setEndpoint(constants.API_PEDALBOARDS, constants.HTTP_GET, modep.getPedalboards);
}) for (var index = 0; index < pedalboards.length; index++) {
.then(modep.getDefaultPedalboard) var id = pedalboards[index].id;
.then(function (data) { setEndpoint(constants.API_PEDALBOARDS + '/' + id, constants.HTTP_GET, modep.getPedalboardById, id)
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(modep.getCurrentPedals) .then(modep.getCurrentPedals)
.then(function (data) { .then(function (pedals) {
setEndpoints(constants.API_PEDALS, data); 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) .then(resolve)
.catch(reject); .catch(reject);

View file

@ -23,7 +23,7 @@ function reset() {
function getBanks() { function getBanks() {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
if (banks) { if (banks != undefined) {
return resolve(banks); return resolve(banks);
} }
// FAKE DATA // 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() { function getPedalboards() {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
if (pedalboards) { if (pedalboards != undefined) {
return resolve(pedalboards); return resolve(pedalboards);
} }
// FAKE DATA // FAKE DATA
@ -90,7 +106,7 @@ function getPedalboards() {
function getDefaultPedalboard() { function getDefaultPedalboard() {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
if (defaultPedalboard) { if (defaultPedalboard != undefined) {
return resolve(defaultPedalboard); return resolve(defaultPedalboard);
} }
getPedalboardByBundle(constants.PEDALBOARD_DEFAULT) getPedalboardByBundle(constants.PEDALBOARD_DEFAULT)
@ -101,7 +117,7 @@ function getDefaultPedalboard() {
function getCurrentPedalboard() { function getCurrentPedalboard() {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
if (currentPedalboard && currentPedalboard.id) { if (currentPedalboard != undefined && currentPedalboard.id != undefined) {
return resolve(currentPedalboard); return resolve(currentPedalboard);
} }
// FAKE DATA // FAKE DATA
@ -116,13 +132,15 @@ function getCurrentPedalboard() {
// PRODUCTION // PRODUCTION
util.httpGET(config.modep.host, config.modep.port, '/pedalboard/current') util.httpGET(config.modep.host, config.modep.port, '/pedalboard/current')
.then(getPedalboardByBundle) .then(getPedalboardByBundle)
.then(resolve) .then(function (pedalboard) {
currentPedalboard = pedalboard;
return resolve(currentPedalboard)
})
.catch(reject); .catch(reject);
}); });
} }
function function getPedalboardById(pedalboardId) {
getPedalboardById(pedalboardId) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
getPedalboards() getPedalboards()
.then(function (pedalboards) { .then(function (pedalboards) {
@ -132,7 +150,7 @@ getPedalboardById(pedalboardId) {
} }
return resolve(pedalboards[index]); 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); .catch(reject);
}); });
@ -140,13 +158,14 @@ getPedalboardById(pedalboardId) {
function getPedalboardByBundle(pedalboardBundle) { function getPedalboardByBundle(pedalboardBundle) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
getPedalboards() if (pedalboardBundle == undefined) {
.then(function (pedalboards) { getDefaultPedalboard()
if (!pedalboardBundle) {
return getDefaultPedalboard()
.then(resolve) .then(resolve)
.catch(reject); .catch(reject);
return;
} }
getPedalboards()
.then(function (pedalboards) {
for (var index = 0; index < pedalboards.length; index++) { for (var index = 0; index < pedalboards.length; index++) {
if (pedalboards[index].bundle != pedalboardBundle) { if (pedalboards[index].bundle != pedalboardBundle) {
continue; continue;
@ -324,28 +343,28 @@ function setPedalboard(pedalboard) {
if (!pedalboard || !pedalboard.bundle) { if (!pedalboard || !pedalboard.bundle) {
return reject('error: no bundle set for pedalboard'); return reject('error: no bundle set for pedalboard');
} }
getCurrentPedalboard()
.then(function (currentPedalboard) {
if (pedalboard.id == currentPedalboard.id) { if (pedalboard.id == currentPedalboard.id) {
return resolve('pedalboard \'' + pedalboard.id + '\' is already active'); return resolve('pedalboard \'' + pedalboard.id + '\' is already active');
} }
logger.debug("DERP") })
.then(reset)
reset() .then(util.httpPOST(config.modep.host, config.modep.port, '/pedalboard/load_bundle/?bundlepath=' + pedalboard.bundle))
.then(logger.debug("RESETTED")) .then(function () {
.catch(logger.error); currentPedalboard = pedalboard
parseCurrentPedalboard(currentPedalboard)
})
// reset() .then(resolve)
// .then(function () { .catch(reject);
// util.httpPOST(config.modep.host, config.modep.port, '/pedalboard/load_bundle/?bundlepath=' + pedalboard.bundle)
// })
// .then(getCurrentPedalboard)
// .catch(reject);
}); });
} }
module.exports = { module.exports = {
getBanks, getBanks,
getBankById,
getPedalboards, getPedalboards,
getPedalboardById,
getDefaultPedalboard, getDefaultPedalboard,
getCurrentPedalboard, getCurrentPedalboard,
getCurrentPedals, getCurrentPedals,

View file

@ -22,6 +22,9 @@ function start() {
function handleRequests() { function handleRequests() {
server.on('request', function (request, response) { server.on('request', function (request, response) {
logger.request(request); logger.request(request);
if (request.url.endsWith('/')) {
request.url = request.url.substring(0, request.url.length - 1);
}
var endpoint = api.getEndpoints().get(request.url); var endpoint = api.getEndpoints().get(request.url);
if (!endpoint) { if (!endpoint) {
var msg = 'endpoint \'' + request.url + '\' not defined'; var msg = 'endpoint \'' + request.url + '\' not defined';
@ -38,11 +41,33 @@ function handleRequests() {
return; return;
} }
if (request.method == constants.HTTP_GET) { 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; return;
} }
if (request.method == constants.HTTP_POST) { 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; return;
} }
var msg = 'endpoint \'' + request.url + '\' does not have any handlers for ' + request.method + ' requests'; var msg = 'endpoint \'' + request.url + '\' does not have any handlers for ' + request.method + ' requests';
@ -52,54 +77,37 @@ function handleRequests() {
}); });
} }
function handleGET(response, endpoint) { function handleGET(endpoint) {
var data = endpoint.data; return new Promise(function (resolve, reject) {
if (!data) { if (!endpoint.method) {
response.writeHead(500); return reject('error: no method defined for endpoint \'' + endpoint.url + '\'');
response.end('error: could not get data for endpoint')
return;
} }
response.writeHead(200); endpoint.method(endpoint.args)
response.end(JSON.stringify(data)); .then(function (data) {
return resolve(JSON.stringify(data))
})
.catch(reject);
});
} }
function handlePOST(request, response, endpoint) { function handlePOST(request, response) {
return new Promise(function (resolve, reject) {
if (request.url.startsWith(constants.API_PEDALS)) { if (request.url.startsWith(constants.API_PEDALS)) {
handlePOSTPedals(request) handlePOSTPedals(request)
.then(function (msg) { .then(resolve)
response.writeHead(200); .catch(reject);
response.end(msg);
})
.catch(function (err, code) {
if (!code) {
code = 500;
}
response.writeHead(code);
response.end(err);
logger.error(err);
});
return; return;
} }
if (request.url.startsWith(constants.API_PEDALBOARDS)) { if (request.url.startsWith(constants.API_PEDALBOARDS)) {
handlePOSTPedalboards(request) handlePOSTPedalboards(request)
.then(function (msg) { .then(resolve)
response.writeHead(200); .catch(reject);
response.end(msg);
})
.catch(function (err, code) {
if (!code) {
code = 500;
}
response.writeHead(code);
response.end(err);
logger.error(err);
});
return; return;
} }
var msg = 'endpoint \'' + request.url + '\' is not yet implemented for ' + request.method + ' requests'; var msg = 'endpoint \'' + request.url + '\' is not yet implemented for ' + request.method + ' requests';
response.writeHead(405); response.writeHead(405);
response.end(msg); response.end(msg);
});
} }
function getPOSTParams(request) { function getPOSTParams(request) {

View file

@ -1,6 +1,6 @@
const logger = require('./logger.js'); const logger = require('./logger.js');
const http = require('http'); const http = require('http');
const { HTTP_GET } = require('./constants.js'); const { HTTP_GET, HTTP_POST } = require('./constants.js');
function timeDiff(startTime) { function timeDiff(startTime) {
if (startTime instanceof Date) { if (startTime instanceof Date) {
@ -50,7 +50,11 @@ function httpRequest(host, port, path, method, args) {
if (!responseData) { if (!responseData) {
return resolve(); return resolve();
} }
try {
return resolve(JSON.parse(responseData)); return resolve(JSON.parse(responseData));
} catch (err) {
return resolve(responseData);
}
}); });
}); });
request.on('error', function (err) { request.on('error', function (err) {