feat(web): implement web server foundation
This commit is contained in:
33
src/web/router.ts
Normal file
33
src/web/router.ts
Normal 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 });
|
||||
}
|
||||
Reference in New Issue
Block a user