-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
43 lines (35 loc) · 1.05 KB
/
index.ts
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
// run a prompt on the container
const Pages = {
HOME: Bun.file("./src/client/index.html"),
};
class FileResponse extends Response {
static isStaticAsset(url: URL): boolean {
return url.pathname.endsWith(".js") || url.pathname.endsWith(".css");
}
static assetAt(path: string): FileResponse {
const assetFilePath = `./src/client${path}`;
console.log("[FileResponse] serving asset: ", assetFilePath);
return new FileResponse(assetFilePath);
}
constructor(path: string) {
const file = Bun.file(path);
const info = file.type;
console.log("[FileResponse] info:", info);
super(file, {
headers: {},
});
}
}
const app = Bun.serve({
port: 3000,
async fetch(request, server) {
console.log("[app] server:", request.url);
const url = new URL(request.url);
const pathName = url.pathname;
if (FileResponse.isStaticAsset(url)) {
return FileResponse.assetAt(pathName);
}
return new Response(Pages.HOME);
},
});
console.log("[app] now listening on", `http://${app.hostname}:${app.port}`);