-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.ts
78 lines (62 loc) · 2.31 KB
/
app.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
import { Hono } from "https://deno.land/x/hono@v3.11.6/mod.ts"
import { cors, serveStatic } from "https://deno.land/x/hono@v3.11.6/middleware.ts"
import { streamSSE } from "https://deno.land/x/hono@v3.11.6/helper/streaming/index.ts"
const db = await Deno.openKv()
const app = new Hono()
let i = 0
interface LastVisit {
country: string
city: string
flag: string
}
app.use(cors())
app.get('/', serveStatic({ path: './index.html' }))
app.post('/visit', async (c) => {
const { city, flag, country } = await c.req.json<LastVisit>()
await db.atomic()
.set(["lastVisit"], { country, city, flag })
.sum(["visits"], 1n)
.commit()
return c.json({ message: 'ok' })
})
app.get('/visit', (c) => {
return streamSSE(c, async (stream) => {
const watcher = db.watch([["lastVisit"]])
for await (const entry of watcher) {
const { value } = entry[0]
if (value != null) {
await stream.writeSSE({ data: JSON.stringify(value), event: 'update', id: String(i++) })
}
}
// while (true) {
// const { value } = await db.get(["visits"])
// await stream.writeSSE({ data: Number(value).toString(), event: 'update', id: String(i++) })
// await stream.sleep(1000)
// // const message = `Son las ${new Date().toLocaleTimeString()}`
// // await stream.writeSSE({ data: message, event: 'update', id: String(i++) })
// // await stream.sleep(1000)
// }
})
})
// app.get('/counter', (c) => {
// return streamSSE(c, async (stream) => {
// const visitsKey = ["visits"]
// const listOfKeysToWatch = [visitsKey]
// const watcher = db.watch(listOfKeysToWatch)
// for await (const entry of watcher) {
// const { value } = entry[0]
// if (value != null) {
// await stream.writeSSE({ data: value.toString(), event: 'update', id: String(i++) })
// }
// }
// // while (true) {
// // const { value } = await db.get(["visits"])
// // await stream.writeSSE({ data: Number(value).toString(), event: 'update', id: String(i++) })
// // await stream.sleep(1000)
// // // const message = `Son las ${new Date().toLocaleTimeString()}`
// // // await stream.writeSSE({ data: message, event: 'update', id: String(i++) })
// // // await stream.sleep(1000)
// // }
// })
// })
Deno.serve(app.fetch)