-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-server.coffee
executable file
·108 lines (87 loc) · 3.81 KB
/
http-server.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env coffee
express = require 'express'
app = express()
bodyParser = require 'body-parser'
xml2js = require 'xml2js'
{dbConnect, Subscription, SentMessageHash, ValidationError} = require './db'
HTTP_PORT = process.env.HTTP_PORT ? process.env.PORT ? 8080
app.use bodyParser.json()
app.use bodyParser.urlencoded(extended: true) # extended allows nested objects
# clients send HTTP POST to this URL in order to register for push notifications
app.post '/registerclient', (req, res) ->
console.log "registerclient from #{ req.ip }: %j", req.body
# client info in JSON: push client id, routes (lines)
if req.body.registration_id? and req.body.sections? and Array.isArray(req.body.sections)
# remove possible old client route data
promise = Subscription.remove(clientId: req.body.registration_id).exec()
# remove also old sent messages, which means that the messages
# can be sent again to the client if it registers again
SentMessageHash.remove(clientId: req.body.registration_id).exec (err) ->
console.error err if err
# make subscription objects (concurrently with remove operation)
subscriptions =
for sec in req.body.sections
clientId: req.body.registration_id
category: sec.category
line: sec.line
startTime: sec.startTime
endTime: sec.endTime
promise
.then ->
# create subscriptions in database
Subscription.create subscriptions
.onFulfill ->
res.status(200).end()
.onReject (err) ->
if err instanceof ValidationError
# request POST data failed validation
res.status(400).end()
console.warn "POST data failed validation:", err
else
console.error err
res.status(500).end()
else
# request POST data is invalid
res.status(400).end()
console.warn "invalid POST data"
# clients should be able to deregister from all push notifications
app.post '/deregisterclient', (req, res) ->
console.log "deregisterclient from #{ req.ip }: %j", req.body
# body should contain GCM registration_id,
if req.body.registration_id?
# remove client's subscriptions from database
Subscription.remove(clientId: req.body.registration_id).exec()
.onFulfill ->
# remove also old sent messages, which means that the messages
# can be sent again to the client if it registers again
SentMessageHash.remove(clientId: req.body.registration_id).exec (err) ->
console.error err if err
res.status(200).end()
.onReject (err) ->
console.error err
res.status(500).end()
else
# request POST data is invalid
res.status(400).end()
console.warn "invalid POST data"
# if enabled, send test message to registered clients
if process.env.TEST_PUSH?.toLowerCase() not in [undefined, 'false', 'no', 'off', '0']
{sendTestMessage} = require './message-sender'
console.log "/send-test-message enabled"
app.post '/send-test-message', (req, res) ->
if req.body?.msg?
sendTestMessage req.body.msg, req.body.lines, req.body.category
res.status(200).end()
else
res.set 'Content-Type', 'text/plain'
res.status(400).send('Error: msg field not in request body')
start = ->
console.log "Listening on port #{ HTTP_PORT }"
app.listen HTTP_PORT
module.exports =
start: start
if require.main == module
dbConnect()
.onFulfill(start)
.onReject ->
process.exit 2