import { createServer, type IncomingMessage, type ServerResponse } from "node:http"; import { applyCorsHeaders } from "./cors.ts"; import { serveStatic } from "./static.ts"; import { handleChatRoute } from "../routes/chat.ts"; import { handleExtensionUiRoute } from "../routes/extension-ui.ts"; import { handleCommandsRoute } from "../routes/commands.ts"; import { handleModelsRoute } from "../routes/models.ts"; import { handleSessionsRoute } from "../routes/sessions.ts"; import { handleSettingsRoute } from "../routes/settings.ts"; import { handleWebuiConfigRoute } from "../routes/webui-config.ts"; import type { WebUiContext } from "../types/context.ts"; export function createWebUiServer(ctx: WebUiContext) { return createServer((req: IncomingMessage, res: ServerResponse) => { const url = new URL(req.url!, `http://localhost:${ctx.config.port}`); const pathname = url.pathname; if (pathname.startsWith("/api/") && applyCorsHeaders(req, res)) { return; } if (req.method === "GET" && !pathname.startsWith("/api/")) { serveStatic(ctx.config.paths.publicDir, pathname, req, res, true); return; } if (handleChatRoute(req, res, ctx, pathname)) return; if (handleExtensionUiRoute(req, res, ctx, pathname)) return; if (handleSessionsRoute(req, res, ctx, pathname)) return; if (handleModelsRoute(req, res, ctx, pathname)) return; if (handleWebuiConfigRoute(req, res, ctx, pathname, url)) return; if (handleSettingsRoute(req, res, ctx, pathname)) return; if (handleCommandsRoute(req, res, ctx, pathname)) return; res.writeHead(404); res.end("Not found"); }); }