-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
44 lines (37 loc) · 1.45 KB
/
index.ts
File metadata and controls
44 lines (37 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { join } from "path";
import { readdir } from "fs/promises";
const ROOT = import.meta.dir;
const PORT = 3000;
function mimeType(path: string): string {
if (path.endsWith(".html")) return "text/html";
if (path.endsWith(".css")) return "text/css";
if (path.endsWith(".js")) return "application/javascript";
if (path.endsWith(".map")) return "application/json";
if (path.endsWith(".png") || path.endsWith(".jpg")) return "image/png";
return "text/plain";
}
async function buildIndexHtml(): Promise<string> {
const template = await Bun.file(join(ROOT, "index.html")).text();
const slidesDir = join(ROOT, "src", "slides");
const files = (await readdir(slidesDir))
.filter(f => f.endsWith(".html"))
.sort();
const parts = await Promise.all(files.map(f => Bun.file(join(slidesDir, f)).text()));
return template.replace("<!-- SLIDES -->", parts.join("\n\n"));
}
Bun.serve({
port: PORT,
async fetch(req) {
const url = new URL(req.url);
const pathname = url.pathname;
if (pathname === "/" || pathname === "/index.html") {
return new Response(await buildIndexHtml(), {
headers: { "Content-Type": "text/html" },
});
}
const file = Bun.file(join(ROOT, pathname));
if (!await file.exists()) return new Response("Not Found", { status: 404 });
return new Response(file, { headers: { "Content-Type": mimeType(pathname) } });
},
});
console.log(`Presentation running at http://localhost:${PORT}`);