/
server.js
325 lines (282 loc) · 11.7 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
(function () {
'use strict';
/*jshint node:true*/
var redisclient = require('redis').createClient(process.env.REDIS_URL || {
host: '127.0.0.1',
port: 6379
});
var express = require('express');
const Server = require('socket.io');
const socket = require("socket.io");
var bodyParser = require('body-parser');
var compression = require('compression');
var Queue = require('bull');
// Set the Redis server instance either local or the Heroku one since this is deployed mostly on Heroku.
var ThreeDQueue = new Queue('3D-proc', (process.env.REDIS_URL || {
host: '127.0.0.1',
port: 6379
}));
require("dotenv").config();
// Once a job is completed, then send a message via a socket.
ThreeDQueue.on('completed', function (job, synthesisid) {
// A job successfully completed with a `result`.
sendStdMsg(synthesisid, synthesisid);
}).on('progress', function (job, progressdata) {
console.log(progressdata.percent, progressdata.synthesisid);
sendProgressMsg(progressdata.synthesisid, progressdata.percent);
// Job progress updated!
});
// Report Progress suing
// ThreeDQueue.on('completed', function(job, synthesisid) {
// // A job successfully completed with a `result`.
// sendStdMsg(synthesisid, synthesisid);
// });
var url = require('url');
var req = require('request');
var async = require('async');
var tools = require('./3Dprocessor/tools');
var yargs = require('yargs').options({
'port': {
'default': 5000,
'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.'
}
});
var argv = yargs.argv;
if (argv.help) {
return yargs.showHelp();
}
// eventually this mime type configuration will need to change
// https://github.com/visionmedia/send/commit/d2cb54658ce65948b0ed6e5fb5de69d022bef941
// *NOTE* Any changes you make here must be mirrored in web.config.
var mime = express.static.mime;
mime.define({
'application/json': ['czml', 'json', 'geojson', 'topojson'],
'model/vnd.gltf+json': ['gltf'],
'model/vnd.gltf.binary': ['bgltf', 'glb'],
'text/plain': ['glsl']
});
var app = express();
app.use(compression());
var ejs = require('ejs');
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/views'));
app.use('/assets', express.static('static'));
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
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;
}
var dontProxyHeaderRegex = /^(?: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;
}
var upstreamProxy = argv['upstream-proxy'];
var bypassUpstreamProxyHosts = {};
if (argv['bypass-upstream-proxy-hosts']) {
argv['bypass-upstream-proxy-hosts'].split(',').forEach(function (host) {
bypassUpstreamProxyHosts[host.toLowerCase()] = true;
});
}
ThreeDQueue.process(5, __dirname + '/processor.js')
app.post('/getthreeddata', function (request, response) {
var synthesisid = request.body.synthesisid;
async.map([synthesisid], function (sid, done) {
redisclient.get(sid, function (err, results) {
if (err || results == null) {
return done(null, JSON.stringify({
"finalGeoms": "",
"center": ""
}));
} else {
return done(null, results);
}
});
},
function (error, op) {
//only OK once set
op = JSON.parse(op);
response.contentType('application/json');
response.send({
"status": 1,
"final3DGeoms": op.finalGeoms,
"center": op.center,
});
});
});
app.get('/', function (request, response) {
var opts = {};
if (request.query.apitoken && request.query.projectid && request.query.synthesisid && request.query.cteamid) {
// synthesis ID is given
opts = {
'apitoken': request.query.apitoken,
'projectid': request.query.projectid,
'synthesisid': request.query.synthesisid,
'cteamid': request.query.cteamid
};
var baseurl = (process.env.PORT) ? 'https://www.geodesignhub.com/api/v1/projects/' : 'http://local.test:8000/api/v1/projects/';
var apikey = request.query.apitoken;
var cred = "Token " + apikey;
var projectid = request.query.projectid;
var cteamid = request.query.cteamid;
var synthesisid = request.query.synthesisid;
var synprojectsurl = baseurl + projectid + '/cteams/' + cteamid + '/' + synthesisid + '/';
var systemsurl = baseurl + projectid + '/systems/';
var boundsurl = baseurl + projectid + '/bounds/';
var URLS = [synprojectsurl, boundsurl, systemsurl];
async.map(URLS, function (url, done) {
req({
url: url,
headers: {
"Authorization": cred,
"Content-Type": "application/json"
}
}, function (err, response, body) {
if (err || response.statusCode !== 200) {
return done(err || new Error());
}
return done(null, JSON.parse(body));
});
}, function (err, results) {
if (err) return response.sendStatus(500);
var gj = JSON.stringify(results[0]);
var bounds = results[1];
var sys = results[2];
opts['result'] = gj;
opts['systems'] = JSON.stringify(sys);
var rfc = {
"type": "FeatureCollection",
"features": []
};
opts['roads'] = JSON.stringify(rfc);
async.map([synthesisid], function (sid, done) {
redisclient.get(sid, function (err, results) {
if (err || results == null) {
return done(null, JSON.stringify({
"finalGeoms": "",
"center": "0"
}));
} else {
console.log('getting');
return done(null, results);
}
});
},
function (redis_error, redis_op) {
//only OK once set
if (redis_error) return response.sendStatus(500);
var r_op = JSON.parse(redis_op);
if (r_op.center === "0") {
const newLocal = 'sending to q';
ThreeDQueue.add({
"gj": results[0],
// "rfc": rfc,
"sys": sys,
"synthesisid": synthesisid
});
}
opts['final3DGeoms'] = JSON.stringify(r_op.finalGeoms);
opts['center'] = r_op.center;
opts['bing_key'] = process.env.BING_KEY || 'bing-key';
response.render('index', opts);
});
// opts['final3DGeoms'] = JSON.stringify(final3DGeoms);
// opts['center'] = JSON.stringify(center);
// });
});
} else {
response.status(400);
response.send('Please pass all the valid parameters.');
}
});
var server = app.listen(process.env.PORT || 5001); // for Heroku
const io = socket(server);
io.on('connection', function (socket) {
socket.on('room', function (room) {
socket.join(room);
sendWelcomeMsg(room);
});
socket.on('message', function (msg) {
var room = msg.room;
var data = msg.data;
sendStdMsg(room, data);
});
});
function sendWelcomeMsg(room) {
io.sockets.in(room).emit('welcome', 'Joined ' + room);
}
function sendStdMsg(room, synthesisid) {
io.sockets.in(room).emit('message', {
'type': 'message',
'synthesisid': synthesisid
});
}
function sendProgressMsg(room, percentcomplete) {
io.sockets.in(room).emit('message', {
'type': 'progress',
'percentcomplete': percentcomplete
});
}
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.');
});
var isFirstSig = true;
process.on('SIGINT', function () {
if (isFirstSig) {
console.log('Cesium development server shutting down.');
server.close(function () {
process.exit(0);
});
isFirstSig = false;
} else {
console.log('Cesium development server force kill.');
process.exit(1);
}
});
})();