-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
67 lines (61 loc) · 1.44 KB
/
index.js
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
const paths = [
{
"point": "/robots.txt",
"data": import(`./src/static/robots.txt`),
"type": "text/plain"
},
{
"point": "/infodatabase.css",
"data": import(`./src/static/infodatabase.css`),
"type": "text/css"
},
{
"point": "/",
"run": import(`./src/paths`),
},
{
"point": "/about",
"run": import(`./src/paths/about`),
},
{
"start": "/article",
"run": import(`./src/paths/article`),
},
{
"start": "/references",
"run": import(`./src/paths/references`),
},
{
"start": "/search",
"run": import(`./src/paths/search`),
}
]
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
let url = new URL(request.url)
let uri = url.pathname;
// this section seems very hacky, would like to replace it with something better
async function serve(point, uri) {
if (point.data) {
return new Response((await point.data).default, {
headers: { 'content-type': point.type }
})
} else {
return await (await point.run).default(uri)
}
}
for (let point of paths) {
if (point.point) {
if (point.point == uri) return await serve(point, url)
} else if (point.start) {
if (uri.startsWith(point.start)) {
return await serve(point, url)
}
}
}
return new Response("404!", {
headers: { 'content-type': 'text/html' }, status: 404
})
}