-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
55 lines (45 loc) · 1.23 KB
/
main.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
import { Application, Router } from "https://deno.land/x/[email protected]/mod.ts"
import {
errorResponse,
getEvents,
isAuthorized,
logEvent,
setHeaders
} from './helpers.ts'
import { Event } from './helpers.ts'
const port = 8000
const db = await Deno.openKv()
const app = new Application()
const router = new Router()
router.get('/', async (ctx) => {
ctx.response.body = 'deno events api running... 🦕'
})
router.post('/events', async (ctx) => {
if (!isAuthorized(ctx)) return
try {
const { source, message, sendNotification } = await ctx.request.body.json()
const event: Event = {
source,
message,
sendNotification: sendNotification ?? false,
createdAt: new Date().toISOString()
}
await logEvent(db, event)
} catch (e) {
errorResponse(ctx, e)
}
setHeaders(ctx)
ctx.response.body = 'event logged'
})
router.get('/events', async (ctx) => {
if (!isAuthorized(ctx)) return
try {
ctx.response.body = await getEvents(db)
} catch (e) {
errorResponse(ctx, e)
}
})
app.use(router.routes())
app.use(router.allowedMethods())
console.log('Running on port: ', port)
await app.listen({ port })