Add persistence, lifecycle improvements, logging and typescript fixes
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
import { IncomingMessage, ServerResponse } from "http";
|
||||
import { parse as parseUrl } from "url";
|
||||
import { WebApi } from "./api";
|
||||
|
||||
export function registerRoutes(server: { on: (event: string, listener: (...args: any[]) => void) => void }, api: WebApi): void {
|
||||
server.on("request", (req: IncomingMessage, res: ServerResponse) => {
|
||||
const url = parseUrl(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 = 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 = 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" }));
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user