generated from whyk-pg/learn-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tsx
42 lines (37 loc) · 1.16 KB
/
main.tsx
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
import { type VNode } from "preact";
import { renderToString } from "preact-render-to-string";
import { Status } from "std/http/http_status.ts";
import { contentType } from "std/media_types/mod.ts";
import { Layout, type PageOptions } from "./layout.tsx";
import { Home, homePageOptions } from "./routes/home.tsx";
import { About, aboutPageOptions } from "./routes/about.tsx";
import { NotFoundError, notFoundErrorPageOptions } from "./routes/404.tsx";
const htmlResponse = (
options: PageOptions,
contents: VNode,
status: Status = Status.OK,
) =>
new Response(
renderToString(
<Layout sitename="Verify Deno Simple App" options={options}>
{contents}
</Layout>,
),
{
status,
headers: { "Content-Type": contentType("html") },
},
);
const router = (req: Request): Response => {
const url = new URL(req.url);
if (url.pathname === "/") return htmlResponse(homePageOptions, <Home />);
if (url.pathname.endsWith("/about")) {
return htmlResponse(aboutPageOptions, <About />);
}
return htmlResponse(
notFoundErrorPageOptions,
<NotFoundError />,
Status.NotFound,
);
};
Deno.serve((req) => router(req));