forked from olenirwing/maphopper
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
131 lines (118 loc) · 3.97 KB
/
index.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
const bunyan = require('bunyan')
const express = require('express')
const helmet = require('helmet')
const proxy = require('express-http-proxy')
const proxyService = express()
const Prometheus = require('./src/prometheus')
const PORT = 8082
const directionsMapper = require('./src/mappers/directions_mapper')
const isoMapper = require('./src/mappers/isochrone_mapper')
const optiMapper = require('./src/mappers/optimization_mapper')
const matrixMapper = require('./src/mappers/matrix_mapper')
const GH_BASE = 'https://graphhopper.com'
const SUCC_MSG = 'Succesful Mapping'
let loggerOptions = { name: 'maphopper' }
var log = bunyan.createLogger(loggerOptions)
// PROMETHEUS
/// The below arguments start the counter functions
proxyService.use(Prometheus.requestCounters)
proxyService.use(Prometheus.responseCounters)
// Enable metrics endpoint
Prometheus.injectMetricsRoute(proxyService)
// Enable collection of default metrics
Prometheus.startCollection()
function logProxyMessage (url) {
log.info(url + ' : request proxied to ' + GH_BASE)
}
function logError (msg, errorCode = 0) {
let errorBody = { code: errorCode }
if (errorCode === 0) {
errorBody = {}
}
log.error(errorBody, msg)
}
proxyService.use(helmet())
proxyService.use('/api/1/route', proxy(GH_BASE, {
proxyReqPathResolver (req) {
let url = req.url.replace('/', '/api/1/route')
logProxyMessage(url)
return url
},
userResDecorator: function (proxyRes, proxyResData, userReq, userRes) {
let data = JSON.parse(proxyResData.toString('utf-8'))
if (userRes.statusCode !== 200) {
data['responseCode'] = userRes.statusCode
logError(data.message, userRes.statusCode)
return data
} else {
if (userReq.query.points_encoded !== 'false') {
let msg = 'points_encoded has to be false'
data = { 'message': msg
}
logError(msg)
return data
}
var mapBoxResponse = directionsMapper.getMapping(data, userReq.query.profile, userReq.query.locale, userReq.query.mapboxkey, userReq.query.geometries)
log.info(SUCC_MSG)
return JSON.stringify(mapBoxResponse)
}
}
})
)
proxyService.use('/api/1/isochrone', proxy(GH_BASE, {
proxyReqPathResolver (req) {
let url = req.url.replace('/', '/api/1/isochrone')
logProxyMessage(url)
return url
},
userResDecorator: function (proxyRes, proxyResData, userReq, userRes) {
let data = JSON.parse(proxyResData.toString('utf-8'))
if (userRes.statusCode !== 200) {
data['responseCode'] = userRes.statusCode
return data
} else {
let res = isoMapper.getMapping(data, userReq.query.time_limit, userReq.query.buckets, userReq.query.contours_colors)
log.info(SUCC_MSG)
return JSON.stringify(res)
}
}
}))
proxyService.use('/api/1/vrp', proxy(GH_BASE, {
proxyReqPathResolver (req) {
let url = req.url.replace('/', '/api/1/vrp/')
logProxyMessage(url)
return url
},
userResDecorator: function (proxyRes, proxyResData, userReq, userRes) {
let data = JSON.parse(proxyResData.toString('utf-8'))
if (userRes.statusCode !== 200 || data.status !== 'finished') {
data['responseCode'] = userRes.statusCode
return data
} else {
let res = optiMapper.getMapping(data)
log.info(SUCC_MSG)
return JSON.stringify(res)
}
}
}))
proxyService.use('/api/1/matrix', proxy(GH_BASE, {
proxyReqPathResolver (req) {
let url = req.url.replace('/', '/api/1/matrix')
logProxyMessage(url)
return url
},
userResDecorator: function (proxyRes, proxyResData, userReq, userRes) {
let data = JSON.parse(proxyResData.toString('utf-8'))
if (userRes.statusCode !== 200) {
data['responseCode'] = userRes.statusCode
return data
} else {
let res = matrixMapper.getMapping(data, userReq.query.out_array, userReq.query.from_point
, userReq.query.to_point, userReq.query.point)
log.info(SUCC_MSG)
return JSON.stringify(res)
}
}
}))
log.info("Maphopper is running...")
proxyService.listen(PORT)