-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.coffee
73 lines (58 loc) · 1.49 KB
/
index.coffee
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
{
PORT = 3000
NODE_ENV = 'development'
SENTRY_DSN_API
} = process.env
Koa = require 'koa'
router = do require 'koa-router'
json = require 'koa-json'
cors = require '@koa/cors'
Sentry = require '@sentry/node'
Event = require './event'
{
slack_payload
post_to_slack
} = require './slack'
#
# Routes
#
router.get '/', (ctx)->
ctx.body = """
Usage:
GET /
this message (🍁 free health check 🇨🇦)
GET /week
display slack payload for the week
POST /week
post this week's events to slack
GET /today
display slack payload for today
POST /today
post today's events to slack
"""
router.get '/week', week = (ctx)->
events = await Event.this_week()
ctx.assert events.length, 404, 'No events this week'
ctx.body = slack_payload events, 'Events this week'
router.post '/week', (ctx)->
await post_to_slack await week ctx
router.get '/today', today = (ctx)->
events = await Event.today()
ctx.assert events.length, 404, 'No events today'
ctx.body = slack_payload events, 'Events today'
router.post '/today', (ctx)->
await post_to_slack await today ctx
#
# Server init
#
app = new Koa
app.use cors()
app.use json()
app.use router.routes()
if dsn = SENTRY_DSN_API
Sentry.init { dsn, environment: NODE_ENV }
app.on 'error', (err, ctx)->
Sentry.withScope (scope)->
scope.addEventProcessor (event)-> Sentry.Handlers.parseRequest event, ctx.request
Sentry.captureException err
app.listen PORT