Rick 156a57866d Initial Commit
Init Commit Backend Server
2020-07-02 01:09:01 +02:00

65 lines
1.8 KiB
JavaScript

"use strict";
class Router {
constructor() {
this.staticRoutes = {};
this.dynamicRoutes = {};
}
/* sets static routes to check for */
addStaticRoute(route, callback) {
this.staticRoutes[route] = callback;
}
/* sets dynamic routes to check for */
addDynamicRoute(route, callback) {
this.dynamicRoutes[route] = callback;
}
getResponse(req, body, sessionID) {
let output = "";
let url = req.url;
let info = {};
/* parse body */
if (body !== "") {
info = json.parse(body);
}
/* remove retry from URL */
if (url.includes("?retry=")) {
url = url.split("?retry=")[0];
}
/* route request */
if (url in this.staticRoutes) {
output = this.staticRoutes[url](url, info, sessionID);
} else {
for (let key in this.dynamicRoutes) {
if (url.includes(key)) {
output = this.dynamicRoutes[key](url, info, sessionID);
}
}
}
/* load files from game cache */
if ("crc" in info) {
let crctest = json.parse(output);
if ("crc" in crctest && info.crc === crctest.crc) {
logger.logWarning("[CRC match]: loading from game cache files");
output = '{"err":0, "errmsg":null, "data":null}';
}
}
/* route doesn't exist or response is not properly set up */
if (output === "") {
logger.logError("[UNHANDLED][" + url + "] request data: " + json.stringify(info));
output = '{"err":404, "errmsg":"UNHANDLED RESPONSE: ' + url + '", "data":null}';
}
return output;
}
}
module.exports.router = new Router();