-
Notifications
You must be signed in to change notification settings - Fork 88
/
server.js
63 lines (47 loc) · 1.81 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
"use strict";
// increase the libuv threadpool size to 1.5x the number of logical CPUs.
process.env.UV_THREADPOOL_SIZE = process.env.UV_THREADPOOL_SIZE || Math.ceil(Math.max(4, require('os').cpus().length * 1.5));
var path = require("path");
var raven = require("raven");
var onHeaders = require('on-headers');
var tessera = require("tessera"),
express = require("express"),
cors = require("cors"),
tilelive = require("tilelive-cache")(require("tilelive"), {
size: process.env.CACHE_SIZE,
sources: process.env.SOURCE_CACHE_SIZE
});
var app = express().disable("x-powered-by");
require("tilelive-modules/loader")(tilelive);
var loggingThreshold = +(process.env.LOG_MS || 5000);
var slowLogger = function(req, res, next) {
var startAt = process.hrtime();
onHeaders(res, function () {
if (this.getHeader('X-Response-Time')) {
return;
}
var diff = process.hrtime(startAt);
var ms = diff[0] * 1e3 + diff[1] * 1e-6;
if (ms > loggingThreshold) {
console.log("SLOW REQUEST ", req.originalUrl, " ", ms, " ms");
}
this.setHeader('X-Response-Time', ms.toFixed(3) + 'ms');
});
next();
};
var config = require("./tessera.json");
Object.keys(config).forEach(function(prefix) {
if (config[prefix].timing !== false) {
app.use(prefix, slowLogger);
}
if (config[prefix].cors !== false) {
app.use(prefix, cors());
}
app.use(raven.middleware.express(process.env.SENTRY_DSN));
app.use(prefix, express.static(path.join(__dirname, "node_modules/tessera/public")));
app.use(prefix, express.static(path.join(__dirname, "node_modules/tessera/bower_components")));
app.use(prefix, tessera(tilelive, config[prefix]));
});
app.listen(process.env.PORT || 8080, function() {
console.log("Listening at http://%s:%d/", this.address().address, this.address().port);
});