-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.ts
38 lines (34 loc) · 1.01 KB
/
serve.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
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { renderToString } from "https://deno.land/x/[email protected]/mod.ts";
import { getPage } from "./page.tsx";
export function startServer() {
serve(async (req) => {
const path = new URL(req.url).pathname;
let page;
if (path === "/") {
page = await getPage("sf-live", "sf-qa");
} else {
const match = path.match(/^\/compare\/([-\w.]+)\/([-\w.]+)$/);
if (match) {
const base = match[1];
const head = match[2];
const html = await renderToString(await getPage(base, head));
return new Response(html, {
headers: {
"content-type": "text/html; charset=UTF-8",
},
});
}
}
if (page) {
const html = await renderToString(page);
return new Response(html, {
headers: {
"content-type": "text/html; charset=UTF-8",
},
});
} else {
return new Response("Not found", { status: 404 });
}
});
}