blastoise/routes/static.ts

30 lines
790 B
TypeScript

import { file } from "bun";
import { join } from "path";
import { PUBLIC_DIR } from "../config";
// Serve static files
export async function handleStatic(path: string): Promise<Response | null> {
if (path === "/" || path === "/index.html") {
return new Response(file(join(PUBLIC_DIR, "index.html")), {
headers: { "Content-Type": "text/html" },
});
}
if (path === "/styles.css") {
return new Response(file(join(PUBLIC_DIR, "styles.css")), {
headers: { "Content-Type": "text/css" },
});
}
if (path.endsWith(".js")) {
const jsFile = file(join(PUBLIC_DIR, path.slice(1)));
if (await jsFile.exists()) {
return new Response(jsFile, {
headers: { "Content-Type": "application/javascript" },
});
}
}
return null;
}