This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Server.js
81 lines (63 loc) · 2.35 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
'use strict'
const HOST_NAME = '0.0.0.0'
const PORT = 3000
const DATA_RELOAD_TIMOUT = 1000 * 60
const DataLoader = require('./DataLoader')
const Filter = require('./Filter')
const http = require('http')
const url = require('url')
console.log('Loading data')
DataLoader.loadData()
.then(data => {
const filter = new Filter(data)
setInterval(() => {
console.log('Reloading data')
DataLoader.loadData()
.then(data => {
filter.data = data
})
.catch(err => {
console.log(err)
})
}, DATA_RELOAD_TIMOUT)
const server = http.createServer((req, res) => {
const urlParts = url.parse(req.url, true)
const query = urlParts.query
if (urlParts.pathname === '/api/v1/nearby' && query.lat && query.long && query.r) {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
let args = [query.lat, query.long, query.r]
res.end(
JSON.stringify({
pokemons: filter.getPokemonsNerby(...args),
gyms: filter.getGymsNerby(...args),
pokestops: filter.getPokestopsNerby(...args)
})
)
return
}
if (urlParts.pathname === '/api/v1/nearby_rect' &&
query.topLat && query.topLong && query.bottomLat && query.bottomLong
) {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
let coordinates = [query.topLat, query.topLong, query.bottomLat, query.bottomLong]
res.end(
JSON.stringify({
pokemons: filter.getPokemonsInRect(...coordinates),
gyms: filter.getGymsInRect(...coordinates),
pokestops: filter.getPokestopsInRect(...coordinates)
})
)
return
}
res.statusCode = 500
res.end()
})
server.listen(PORT, HOST_NAME, () => {
console.log(`Server running at http://${HOST_NAME}:${PORT}/`)
})
})
.catch(err => {
console.log(err)
})