forked from anomalyco/models.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
89 lines (81 loc) · 2.09 KB
/
Copy pathserver.ts
File metadata and controls
89 lines (81 loc) · 2.09 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import Index from "../index.html";
import { normalizeLogoSvg } from "./logo.js";
import { Providers, Rendered } from "./render";
import path from "path";
Bun.serve({
port: 16_000,
routes: {
"/": Index,
"/api.json": () => {
return Response.json(Providers, {
headers: {
"Cache-Control": "public, max-age=3600",
},
});
},
"/assets/*": (req) => {
const file = Bun.file(
path.join(import.meta.dir, new URL(req.url).pathname)
);
return new Response(file);
},
"/logos/*": async (req) => {
const url = new URL(req.url);
const provider = url.pathname.split("/")[2].replace(".svg", "");
const logoPath = path.join(
import.meta.dir,
"..",
"..",
"..",
"providers",
provider,
"logo.svg"
);
const defaultLogoPath = path.join(
import.meta.dir,
"..",
"..",
"..",
"providers",
"logo.svg"
);
let file = Bun.file(logoPath);
if (!(await file.exists())) {
file = Bun.file(defaultLogoPath);
}
return new Response(normalizeLogoSvg(await file.text()), {
headers: {
"Content-Type": "image/svg+xml",
"Cache-Control": "public, max-age=3600",
},
});
},
},
});
const server = Bun.serve({
development: true,
hostname: "0.0.0.0",
async fetch(req) {
// Reject WebSocket upgrade requests
if (req.headers.get("upgrade") === "websocket") {
return new Response("WebSocket upgrades not supported", {
status: 426,
headers: {
Upgrade: "Required",
},
});
}
const url = new URL(req.url);
url.host = "localhost:16000";
const result = fetch(url.toString(), req);
if (url.pathname !== "/") return result;
let html = await result.then((r) => r.text());
html = html.replace("<!--static-->", Rendered);
return new Response(html, {
headers: {
"Content-Type": "text/html",
},
});
},
});
console.log(`Server running at ${server.hostname}:${server.port}`);