-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
352 lines (302 loc) · 8.8 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
'use strict'
// establish DB connection
var MongoClient = require('mongodb').MongoClient,
express = require('express'),
compression = require('compression'),
url = require('url'),
request = require('request'),
yargs = require('yargs').options({
port: {
default: 8080,
description: 'Port to listen on.'
},
public: {
type: 'boolean',
description: 'Run a public server that listens ' +
'on all interfaces.'
},
'upstream-proxy': {
description: 'A standard proxy server that will be used to' +
' retrieve data. Specify a URL including port, ' +
'e.g. "http://proxy:8000".'
},
'bypass-upstream-proxy-hosts': {
description: 'A comma separated list of hosts that will ' +
'bypass the specified upstream_proxy, ' +
'e.g. "lanhost1,lanhost2"'
},
help: {
alias: 'h',
type: 'boolean',
description: 'Show this help.'
}
}),
argv = yargs.argv,
bypassUpstreamProxyHosts,
dontProxyHeaderRegex,
upstreamProxy,
server,
mime,
app,
projectServiceUrlParts
/***
* featureInfo is stored locally because its not available yet in the converted data
* @type {*[]}
*/
var featureInfo = [
{ "gltfid": "54ee20fc0aeba2353a80330d", "levels": 6, "roof": "gray", "height": "17m", "coordinates": [4.4446946, 51.9125311] },
{ "gltfid": "54ee20ce0aeba2353a803303", "levels": 6, "roof": "brown", "height": "15m", "coordinates": [4.4446946, 51.9115311] },
{ "gltfid": "54ee20ee0aeba2353a80330a", "levels": 3, "roof": "brown", "height": "11m", "coordinates": [4.4442946, 51.9112311] },
{ "gltfid": "54ee21080aeba2353a80330f", "levels": 4, "roof": "red", "height": "14m", "coordinates": [4.4416946, 51.9109311] },
{ "gltfid": "54ee20db0aeba2353a803306", "levels": 5, "roof": "gray", "height": "15m", "coordinates": [4.4461946, 51.9104311] },
{ "gltfid": "54ee20e90aeba2353a803309", "levels": 4, "roof": "gray", "height": "13m", "coordinates": [4.4466946, 51.9101311] },
{ "gltfid": "54ee20f60aeba2353a80330c", "levels": 14, "height": "57m", "roof": "gray", "coordinates": [4.4496946, 51.9115311] },
{ "gltfid": "54ee20f20aeba2353a80330b", "levels": 2, "height": "10m", "roof": "brown", "coordinates": [4.4492946, 51.9112311]}
]
if (argv.help)
return yargs.showHelp()
// Eventually this mime type configuration will need to change
// https://github.com/tj/send/commit/d2cb546
mime = express.static.mime
mime.define({
'application/json': ['czml', 'json', 'geojson', 'topojson', 'gltf'],
'text/plain': ['glsl']
})
app = express()
app.use(compression())
app.use(express.static(__dirname))
function getRemoteUrlFromParam(req) {
var remoteUrl = req.params[0]
if (remoteUrl) {
// add http:// to the URL if no protocol is present
if (!/^https?:\/\//.test(remoteUrl))
remoteUrl = 'http://' + remoteUrl
remoteUrl = url.parse(remoteUrl)
// copy query string
remoteUrl.search = url.parse(req.url).search
}
return remoteUrl
}
dontProxyHeaderRegex = new RegExp(
'^(?:Host|Proxy-Connection|Connection|Keep-Alive|' +
'Transfer-Encoding|TE|Trailer|' +
'Proxy-Authorization|Proxy-Authenticate|Upgrade)$',
'i'
)
function filterHeaders(req, headers) {
var result = {}
// filter out headers that are listed in the regex above
Object
.keys(headers)
.forEach(function (name) {
if (!dontProxyHeaderRegex.test(name)) {
result[name] = headers[name]
}
})
return result
}
upstreamProxy = argv['upstream-proxy']
bypassUpstreamProxyHosts = {}
if (argv['bypass-upstream-proxy-hosts']) {
argv['bypass-upstream-proxy-hosts']
.split(',')
.forEach(function (host) {
bypassUpstreamProxyHosts[host.toLowerCase()] = true
})
}
app.get('/proxy/*', function (req, res, next) {
var remoteUrl,
proxy
// look for request like
// localhost:8080/proxy/http://example.com/file?query=1
remoteUrl = getRemoteUrlFromParam(req)
if (!remoteUrl) {
// look for request like
// localhost:8080/proxy/?http%3A%2F%2Fexample.com%2Ffile%3Fquery%3D1
remoteUrl = Object.keys(req.query)[0]
if (remoteUrl)
remoteUrl = url.parse(remoteUrl)
}
if (!remoteUrl)
return res.send(400, 'No url specified.')
if (!remoteUrl.protocol)
remoteUrl.protocol = 'http:'
if (upstreamProxy && !(remoteUrl.host in bypassUpstreamProxyHosts))
proxy = upstreamProxy
// encoding : null means "body" passed to the callback will be raw bytes
request.get(
{
url: url.format(remoteUrl),
headers: filterHeaders(req, req.headers),
encoding: null,
proxy: proxy
},
function (error, response, body) {
var code = 500
if (response) {
code = response.statusCode
res.header(filterHeaders(req, response.headers))
}
res.send(code, body)
}
)
})
/***
* the getFeatureInfo route returns additional information for a building
* @params: id - buildingID
*/
app.get('/db/getFeatureInfo', function (req, res, next) {
var info = {}
for(var i=0; i<featureInfo.length; i++){
if(featureInfo[i]["gltfid"] == req.query.id){
info = featureInfo[i]
break;
}
}
//{buildingName: 'Rathaus', height: '45m'}
res.status(200).send(info)
/***
* the database is currently not connected because the feature info
* for buildings is not available due to issues with the conversion pipeline
*/
/*
MongoClient.connect(
'mongodb://localhost:27017/cityViz',
function (err, db) {
if (!err) {
console.log('We are connected')
var collection = db.collection('featureInfo')
if (collection != null)
collection.find({"gtlfid": req.query.id}).toArray(function (err, items) {
res.status(200).send({err: err, result: items})
})
else
res
.status(404)
.send({result: 'Could not retrieve data from db...'})
}
})*/
})
/***
* the getScene route queries all scene information from the datavase and retunr it
* @params: none
*/
app.get('/db/getScene', function (req, res, next) {
// Connect to the db
MongoClient.connect(
'mongodb://localhost:27017/cityViz',
function (err, db) {
if (!err) {
console.log('We are connected')
var collection = db.collection('buildings')
if (collection != null)
collection.find({}).toArray(function (err, items) {
res.status(200).send({err: err, result: items})
})
else
res
.status(404)
.send({result: 'Could not retrieve data from db...'})
}
})
})
/***
* this route can be used to project coordinates via http://sampleserver1.arcgisonline.com
* it is not used currently
* @params: values - 2-dimensional array of float values
*/
app.get('/project/*', function (req, res, next) {
var proxy,
serviceUrl = createServiceUrl(req.query.values)
console.log(serviceUrl)
if (upstreamProxy && !(serviceUrl.host in bypassUpstreamProxyHosts))
proxy = upstreamProxy
request.get(
{
url: url.format(serviceUrl),
headers: filterHeaders(req, req.headers),
encoding: null,
proxy: proxy
},
function (error, response, body) {
var code = 500
if (response) {
code = response.statusCode
res.header(filterHeaders(req, response.headers))
}
res.send(code, body)
}
)
})
server = app.listen(
argv.port, argv.public ? undefined : 'localhost',
function () {
if (argv.public) {
console.log(
'Cesium development server running publicly. ' +
' Connect to http://localhost:%d/',
server.address().port
)
}
else {
console.log(
'Cesium development server running locally. ' +
'Connect to http://localhost:%d/',
server.address().port
)
}
})
server.on('error', function (e) {
if (e.code === 'EADDRINUSE') {
console.log('Error: Port %d is already in use, ' +
'select a different port.', argv.port)
console.log('Example: node server.js --port %d', argv.port + 1)
}
else if (e.code === 'EACCES') {
console.log('Error: This process does not have permission to ' +
'listen on port %d.', argv.port)
if (argv.port < 1024)
console.log('Try a port number higher than 1024.')
}
console.log(e)
process.exit(1)
})
server.on('close', function () {
console.log('Cesium development server stopped.')
})
process.on('SIGINT', function () {
server.close(function () {
process.exit(0)
})
})
/**** Project coordinates from 28992 to 4326
http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Geometry/
GeometryServer/project?inSR=28992&
outSR=4326&geometries=%7B%0D%0A%22geometryType
%22%3A%22esriGeometryPoint%22%2C%0D%0A%22geometries%22%3A%5B%7B%22x%22%
3A86693.42%2C+%22y%22%3A+441900.78%7D%5D%0D%0A%7D&f=JSON
****/
projectServiceUrlParts = [
'http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Geometry/' +
'GeometryServer/project?',
'inSR=28992',
'&outSR=4326',
'&geometries={',
'"geometryType":"esriGeometryPoint",',
'"geometries":'
]
//expect array like [ [1.1, 2.1], [2.1, 1.1] ]
function createServiceUrl(array) {
var geometries = [],
serviceUrl
array.forEach(function (point) {
geometries.push({
x: point[0],
y: point[1]
})
})
serviceUrl = projectServiceUrlParts.join('') +
JSON.stringify(geometries) +
'}&f=pjson'
return serviceUrl
}