-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.ls
61 lines (47 loc) · 2.03 KB
/
routes.ls
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
{promises:{to-callback}} = require \async-ls
require! \cli-color
{log-events, projects}:config = require \./config
require! \pipend-spy
# :: a -> [ExpressRoute]
module.exports = do ->
# die :: Response -> Error -> Void
die = (res, err) !->
console.log err.to-string!
res.status 500 .end err.to-string!
return
# for preventing browsers from making an OPTIONS request
* \json-parse-plain-text, \use, (req, res, next) ->
if req.headers[\content-type] != \text/plain
next!
# JSON parse POST body if the content type is text/plain
else
body = ""
req.set-encoding \utf8
req.on \data, (chunk)-> body := body + chunk
req.on \end, ->
try
req.body = JSON.parse body
catch err
return die res, err
next!
* \terminate-empty-request, \use, (req, res, next) ->
if req.body then next! else die res, "no event-object passed in the POST body"
* \record-event, \post, "/:project", (req, res) ->
{project} = req.params
if !projects?[project]
die res, "project #{project} not found"
else
if !!log-events
console.log cli-color.green-bright "#{project} : #{new Date!}"
console.log JSON.stringify req.body, null, 4
console.log ""
# TODO: update to-callback function to avoid consumption of exceptions
err, result <- to-callback ((pipend-spy projects[req.params.project]).record-req req, req.body)
# prevent consumption of exceptions by to-callback
<- set-immediate
if !!err
die res, err
else
res.set \Content-Type, \application/javascript
res.end JSON.stringify result, null, 4
...