This repository has been archived by the owner on Apr 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
server.js
75 lines (63 loc) · 1.78 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
"use strict";
var mime = require('mime');
var restify = require('restify');
var yargs = require('yargs');
yargs.options({
'port': {
'default': 8080,
'description': 'Port to listen on.'
},
'public': {
'type': 'boolean',
'default': false,
'description': 'Run a public server that listens on all interfaces.'
},
'production': {
'type': 'boolean',
'default': false,
'description': 'Run the built version of the code.'
}
});
var argv = yargs.argv;
var server = restify.createServer();
//The built in gzipResponse gzips everything, including compressed files.
//This disables gzipping for specific routes and files.
var gzipResponse = restify.gzipResponse();
server.use(function (req, res, next) {
var url = req.url;
//Tiles are pre-gzipped.
if (/\/3DTiles\/(.*)/.test(url)) {
res.header('Content-Encoding', 'gzip');
next();
return;
}
//Don't gzip things that are likely gzipped already.
var contentType = mime.lookup(url);
if (/^(image|audio|video)\//.test(contentType)) {
next();
return;
}
gzipResponse(req, res, next);
});
server.get(/.*/, restify.serveStatic({
directory: argv.production ? 'build' : 'public',
default: 'index.html',
maxAge: 0
}));
server.listen(argv.port, argv.public ? undefined : 'localhost', function () {
console.log('%s listening at %s', server.name, server.url);
});
var shuttingDown = false;
function shutdown() {
if (shuttingDown) {
return;
}
shuttingDown = true;
console.log("Closing server connections...");
server.close(function () {
console.log("Shutdown successfull.");
process.exit(0);
});
}
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);