-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
171 lines (155 loc) · 4.03 KB
/
server.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict'
/**
* Load modules
*/
const http = require('http')
const express = require('express')
const fs = require('fs')
const request = require('request-promise')
const _ = require('lodash')
/**
* Set port to listening
* @type {Number | String}
*/
const PORT = process.env.port || process.env.PORT || 8080
/**
* Object for cached data from remote server
* @type {Object}
*/
const cachedData = {
buses: null,
time: 0,
}
const security = false
/**
* Setup express app
*/
const app = express()
app.use('/', express.static('assets'))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html')
})
app.get('/busStops', (req, res) => {
// simple security
if (security && !req.headers.referer) return res.status(403).end()
res.sendFile(__dirname + '/busStops.json')
})
app.get('/busRoute', (req, res) => {
const gmvid = req.query.gmvid
// simple security
if (security && (!req.headers.referer || !gmvid)) return res.status(403).end()
getBusRoute(gmvid)
.then(data => {
const response = _.map(data.features, feature => {
return _.map(feature.geometry.coordinates, cords => {
// reverse cords
return [cords[1], cords[0]]
})
})
return res.json(response)
})
.catch(err => {
return res.status(500).json({ msg: 'Error' })
})
})
app.get('/buses', (req, res) => {
// simple security
if (security && !req.headers.referer) return res.status(403).end()
// return res.sendFile(__dirname + '/buses.json')
// if last request was longer than 5s make request again
// if not print data from cached variable
if (Math.abs(new Date().getTime() - cachedData.time) > 5000) {
getBuses()
.then(data => {
cachedData.buses = data
cachedData.time = new Date()
return res.json(data)
})
.catch(err => {
return res.status(500).json({ msg: 'Error' })
})
} else {
return res.json(cachedData.buses)
}
})
/**
* Setup http server
*/
const server = http.createServer(app).listen(PORT, 1, () => {
// Init unpdate busStops.json
updateBusStops()
// Update busStops.json ones per hour
setInterval(() => {
updateBusStops()
}, 1000 * 60 * 60)
})
server.on('request', (req, res) => {
console.log(`${new Date().toISOString().replace(/T|Z/g, ' ')}: ${res.req.method} : ${res.req.url} `)
})
/**
* Get bus stops positions
* @return {Promise}
*/
function getBusStops() {
const obj = {
uri: 'http://www.zditm.szczecin.pl/json/slupki.inc.php',
json: true,
}
return new Promise((resolve, reject) => {
request(obj)
.then(res => {
let data = _.uniqBy(res, 'nrzespolu')
return resolve(data)
})
.catch(err => {
return reject(err)
})
})
}
/**
* Get current position of the buses
* @return {Promise}
*/
function getBuses() {
const obj = {
uri: 'http://www.zditm.szczecin.pl/json/pojazdy.inc.php',
json: true,
}
return new Promise((resolve, reject) => {
request(obj)
.then(res => {
return resolve(res)
})
.catch(err => {
return reject(err)
})
})
}
/**
* Get position of the bus's route
* @return {Promise}
*/
function getBusRoute(gmvid) {
const obj = {
uri: `http://www.zditm.szczecin.pl/json/trasy.inc.php?gmvid=${gmvid}`,
json: true,
}
return new Promise((resolve, reject) => {
request(obj)
.then(res => {
return resolve(res)
})
.catch(err => {
return reject(err)
})
})
}
/**
* Update bus stops positions
* @return {Promise}
*/
function updateBusStops() {
return getBusStops().then(data => {
fs.writeFileSync('busStops.json', JSON.stringify(data, null, 4))
})
}