-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
52 lines (46 loc) · 1.53 KB
/
index.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
const blog_serve_dir = 'generated-blog-site'
function serve_blog(path: String): Response {
if (path.endsWith('/')) {
path += 'index.html'
} else if (!path.substring(path.lastIndexOf('/') + 1).includes('.')) {
path += '/index.html'
}
const maybe_file = blog_serve_dir + path
return new Response(Bun.file(maybe_file))
}
let visit_log: Map<string, [Date, string][]> = new Map()
function visit_log_dump(): string {
const s: string[] = []
visit_log.forEach((val, key) => {
s.push(key + ':\n ' + val.join('\n '))
})
return s.join('\n')
}
Bun.serve({
hostname: '0.0.0.0',
port: '23333',
fetch(request, server) {
const req_url = new URL(request.url)
const req_path = req_url.pathname
const req_ip = server.requestIP(request)
if (req_ip !== null) {
const from = req_ip.address + ':' + req_ip.port
const new_entry: [Date, string] = [new Date(), req_path]
const lst = visit_log.get(from)
if (lst === undefined) {
visit_log.set(from, [new_entry])
} else {
lst.push(new_entry)
}
}
if (req_path === '/statistics') {
return new Response(visit_log_dump())
} else if (req_path === '/blog') {
return serve_blog('/')
} else if (req_path.startsWith('/blog/')) {
return serve_blog(req_path.substring(5))
} else {
return new Response(Bun.file('home/index.html'))
}
},
})