-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.coffee
94 lines (72 loc) · 2.62 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
express = require 'express'
app = express.createServer()
env = process.env
moment = require 'moment'
api = require './lib/api'
mongoose = require 'mongoose'
connection = mongoose.createConnection('mongodb://localhost/buslog')
locationLogger = (require './lib/log').createLogger connection
# Setup configuration
app.use express.errorHandler { showStacktrace: true, dumpExceptions: true }
app.use express.logger 'dev'
app.use express.bodyParser()
app.use express.static(__dirname + '/public')
app.set 'view engine', 'jade'
app.enable 'jsonp callback'
buildOptions = (req) ->
{ range, from, to } = req.query
switch range
when 'now' then return {
from: moment().subtract 'minutes', 15
to: moment().add 'hours', 1
}
when 'restOfDay' then return {
from: moment().subtract 'minutes', 15
to: moment().endOf('day').add 'hours', 2
}
when 'day' then return {
from: moment().startOf 'day'
to: moment().endOf('day').add 'hours', 2
}
from: if from then moment from else moment().startOf 'day'
to: if to then moment to else moment().endOf('day').add 'hours', 2
timeRange = (req, res, next) ->
{ from, to } = buildOptions req
req.params.from = from
req.params.to = to
next()
location = (req, res, next) ->
{ latitude, longitude, radius } = req.query
req.params.latitude = latitude
req.params.longitude = longitude
req.params.radius = radius or 500
next()
app.all '*', (req, res, next) ->
res.header 'Access-Control-Allow-Origin', '*'
next()
app.get '/stops/:id', timeRange, (req, res) ->
api.stop req.params.id, req.params, (err, stop) ->
if err
res.send 404
return
res.json stop
app.get '/stops/', (req, res) ->
api.stops (err, stops) ->
res.json stops
# TODO: Rename this to /stops?near=latlng
app.get '/nearest', location, (req, res) ->
api.nearest req.params.latitude, req.params.longitude, { radius: req.params.radius }, (err, nearest) ->
res.json nearest
app.get '/buses', [location, timeRange], (req, res) ->
locationLogger req.query
api.nearestRoutes req.params.latitude, req.params.longitude, req.params, (err, times) ->
res.json times
app.get '/between', (req, res) ->
from = req.query.from.split ','
to = req.query.to.split ','
api.betweenWithTimes { latitude: from[0], longitude: from[1] }, { latitude: to[0], longitude: to[1] }, (err, routes) ->
res.send routes
# Listen
port = env?.PORT or 3000
app.listen port
console.log "Express server listening on port #{port}"