59 lines
1.5 KiB
JavaScript
59 lines
1.5 KiB
JavaScript
|
const AudioServer = require("./AudioServer");
|
||
|
|
||
|
class Api {
|
||
|
|
||
|
constructor() {
|
||
|
this.get = new Map();
|
||
|
this.post = new Map();
|
||
|
this.#setup();
|
||
|
}
|
||
|
|
||
|
async handleRequest(request) {
|
||
|
if (request === undefined) {
|
||
|
return this.#createRequestResult(500);
|
||
|
}
|
||
|
const fn = this.#getFunction(request.url, request.method.toLowerCase());
|
||
|
if (fn === undefined) {
|
||
|
return this.#createRequestResult(501);
|
||
|
}
|
||
|
await fn();
|
||
|
return this.#createRequestResult();
|
||
|
}
|
||
|
|
||
|
#createRequestResult(code, message) {
|
||
|
if (code === undefined) {
|
||
|
code = 200;
|
||
|
}
|
||
|
const result = {
|
||
|
code: code
|
||
|
};
|
||
|
if (message !== undefined) {
|
||
|
result.message = message;
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
#setup() {
|
||
|
this.#registerEndpoint(constants.API_PLAY, constants.REQUEST_METHOD_POST, () => {
|
||
|
new AudioServer('/mnt/kingston/public/LEFTOVER.flac');
|
||
|
});
|
||
|
}
|
||
|
|
||
|
#getFunction(url, method) {
|
||
|
if (url === undefined || method === undefined || this[method] === undefined) {
|
||
|
return;
|
||
|
}
|
||
|
return this[method].get(url);
|
||
|
}
|
||
|
|
||
|
#registerEndpoint(url, method, fn) {
|
||
|
if (url === undefined || method === undefined || fn === undefined || this[method] === undefined) {
|
||
|
return false;
|
||
|
}
|
||
|
this[method].set(url, fn);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
module.exports = Api;
|