feat(web): implement web server foundation

This commit is contained in:
syntaxbullet
2026-01-07 12:40:21 +01:00
parent 022f748517
commit 2a1c4e65ae
13 changed files with 289 additions and 8 deletions

33
src/web/router.ts Normal file
View File

@@ -0,0 +1,33 @@
import { homeRoute } from "./routes/home";
import { healthRoute } from "./routes/health";
import { file } from "bun";
import { join } from "path";
export async function router(request: Request): Promise<Response> {
const url = new URL(request.url);
const method = request.method;
// Serve static files from src/web/public
// We treat any path with a dot or starting with specific assets paths as static file candidates
if (url.pathname.includes(".") || url.pathname.startsWith("/public/")) {
// Sanitize path to prevent directory traversal
const safePath = url.pathname.replace(/^(\.\.[\/\\])+/, '');
const filePath = join(import.meta.dir, "public", safePath);
const staticFile = file(filePath);
if (await staticFile.exists()) {
return new Response(staticFile);
}
}
if (method === "GET") {
if (url.pathname === "/" || url.pathname === "/index.html") {
return homeRoute();
}
if (url.pathname === "/health") {
return healthRoute();
}
}
return new Response("Not Found", { status: 404 });
}