-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.ts
114 lines (99 loc) · 3.98 KB
/
router.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import * as Peko from "peko"
import { resizableImage } from "pekommunity/imagemagick_deno/mod.ts"
import { html, renderToReadableStream, renderToString } from "pekommunity/react/mod.ts"
import { emitTSBundle } from "pekommunity/emit/mod.ts"
import { recursiveReaddir } from "recursiveReadDir"
import { fromFileUrl } from "path"
import { cache } from "./cache.ts"
import { loader } from "./middleware/loader.ts"
import { IMG_RESOLUTIONS } from "./components/config.ts"
import { checkout } from "./handlers/stripe-checkout.ts"
// import { markdown } from "./handlers/markdown.ts"
// const htmlDoc = await Deno.readTextFile(indexUrl)
import { getProducts } from "./utils/gelato.ts"
import Index from "./pages/Index.ts"
import Trade from "./pages/Trade.ts"
import Archive from "./pages/Archive.ts"
export const router = new Peko.Router()
const prod = Deno.args[0] !== "dev"
/* for dev -> */ // const prod = true
const headers = new Headers({
"Cache-Control": prod ? "max-age=600, stale-while-revalidate=86400" : ""
})
console.log("PROD:" + prod)
// gelato test
router.addRoute("/t-shirts", async () => {
return new Response(JSON.stringify(await getProducts("t-shirts"), null, 2), {
headers: {
"Content-Type": "application/json"
}
})
})
// stripe checkout
router.addRoute("/checkout", checkout("price_1NnSybJ5zd0Ft3OG1k45gyj6"))
// PAGES
router.addRoute(
"/",
prod ? loader(renderToString(html`<${Index} />`)) : [],
Peko.ssrHandler(() => renderToReadableStream(html`<${Index} />`), { headers })
)
router.addRoute(
"/trade",
prod ? loader(renderToString(html`<${Trade} />`)) : [],
Peko.ssrHandler(() => renderToReadableStream(html`<${Trade} />`), { headers })
)
router.addRoute(
"/archive",
prod ? loader(renderToString(html`<${Archive} />`)) : [],
Peko.ssrHandler(() => renderToReadableStream(html`<${Archive} />`), { headers })
)
// COMPONENTS
const components = await recursiveReaddir(fromFileUrl(new URL("./components", import.meta.url)))
router.addRoutes(components.map((file): Peko.Route => {
const fileRoute = file.slice(Deno.cwd().length+1).replaceAll(/\\/g, "/")
return {
path: `/${fileRoute}`,
middleware: prod ? Peko.cacher(cache) : [],
handler: emitTSBundle(new URL(`./${fileRoute}`, import.meta.url))
}
}))
// IMAGES
const images = await recursiveReaddir(fromFileUrl(new URL("./public/images", import.meta.url)))
router.addRoutes(images.map((file): Peko.Route => {
const fileRoute = file.slice(Deno.cwd().length+1).replaceAll(/\\/g, "/")
return {
path: `/${fileRoute}`,
middleware: prod ? Peko.cacher(cache) : [],
handler: resizableImage(new URL(`./${fileRoute}`, import.meta.url), IMG_RESOLUTIONS)
}
}))
// SCRIPTS
const scripts = await recursiveReaddir(fromFileUrl(new URL("./public/scripts", import.meta.url)))
router.addRoutes(scripts.map((file): Peko.Route => {
const fileRoute = file.slice(Deno.cwd().length+1).replaceAll(/\\/g, "/")
return {
path: `/${fileRoute}`,
middleware: prod ? Peko.cacher(cache) : [],
handler: Peko.staticHandler(new URL(`./${fileRoute}`, import.meta.url), { headers })
}
}))
// const stories = await recursiveReaddir(fromFileUrl(new URL("./public/stories", import.meta.url)))
// const storyRoutes = await Promise.all(stories.filter(story => story.includes(".md")).map(async (file): Promise<Peko.Route> => {
// const fileRoute = file.slice(Deno.cwd().length+1)
// const content = await Deno.readTextFile(new URL(`./${fileRoute}`, import.meta.url))
// return {
// route: `/${fileRoute.replace(".md", "")}`,
// middleware: Peko.cacher(cache),
// handler: markdown(htmlDoc, content)
// }
// }))
// STYLE
const style = await recursiveReaddir(fromFileUrl(new URL("./public/style", import.meta.url)))
router.addRoutes(style.map((file): Peko.Route => {
const fileRoute = file.slice(Deno.cwd().length+1).replaceAll(/\\/g, "/")
return {
path: `/${fileRoute}`,
middleware: prod ? Peko.cacher(cache) : [],
handler: Peko.staticHandler(new URL(`./${fileRoute}`, import.meta.url), { headers })
}
}))