-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
51 lines (43 loc) · 1.39 KB
/
server.ts
File metadata and controls
51 lines (43 loc) · 1.39 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
45
46
47
48
49
50
51
import { join } from "node:path";
import app from "./dist/server/server.js";
const DIST_CLIENT = join(import.meta.dir, "dist", "client");
const MIME_TYPES: Record<string, string> = {
".js": "application/javascript",
".css": "text/css",
".html": "text/html",
".json": "application/json",
".png": "image/png",
".svg": "image/svg+xml",
".ico": "image/x-icon",
};
function getMimeType(path: string): string {
const ext = path.slice(path.lastIndexOf("."));
return MIME_TYPES[ext] || "application/octet-stream";
}
const port = Number(process.env.PORT) || 5000;
Bun.serve({
port,
async fetch(request) {
const url = new URL(request.url);
if (url.pathname.startsWith("/assets/")) {
const filePath = join(DIST_CLIENT, url.pathname);
const file = Bun.file(filePath);
if (await file.exists()) {
return new Response(file, {
headers: {
"Content-Type": getMimeType(url.pathname),
"Cache-Control": "public, max-age=31536000, immutable",
},
});
}
}
return app.fetch(request);
},
});
console.log(`Guidebook server listening on http://localhost:${port}`);
import("./src/server/services/init.ts")
.then(({ ensureMainPage }) => ensureMainPage())
.catch((err) => console.warn("Failed to create main page:", err));
import("./src/server/services/self-register.ts")
.then(({ registerGuidebook }) => registerGuidebook())
.catch((err) => console.warn("Failed to register:", err));