Files
server-manager/manager/src/web/routes.js
T

57 lines
2.3 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerRoutes = registerRoutes;
const url_1 = require("url");
function registerRoutes(server, api) {
server.on("request", (req, res) => {
const url = (0, url_1.parse)(req.url ?? "/", true);
const pathname = url.pathname ?? "/";
if (pathname === "/api/health") {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(api.getHealth()));
return;
}
if (pathname === "/api/dashboard") {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(api.getDashboard()));
return;
}
if (pathname === "/api/wrappers") {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(api.getWrappers()));
return;
}
if (pathname.startsWith("/api/wrappers/")) {
const id = decodeURIComponent(pathname.split("/").pop() ?? "");
const wrapper = api.getWrapper(id);
if (!wrapper) {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Wrapper not found" }));
return;
}
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(wrapper));
return;
}
if (pathname === "/api/servers") {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(api.getServers()));
return;
}
if (pathname.startsWith("/api/servers/")) {
const id = decodeURIComponent(pathname.split("/").pop() ?? "");
const server = api.getServer(id);
if (!server) {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Server not found" }));
return;
}
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(server));
return;
}
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Not found" }));
});
}