-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
executable file
·77 lines (55 loc) · 2.04 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
#!/usr/bin/env node
"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"),
util = require("util");
var cors = require("cors"),
express = require("express"),
lru = require("lru-cache"),
morgan = require("morgan"),
responseTime = require("response-time"),
tessera = require("tessera"),
tilelive = require("tilelive-cache")(require("@mapbox/tilelive"), {
sources: 75
});
require("tilelive-modules/loader")(tilelive);
var API_BASE_URL = process.env.API_BASE_URL || "http://fieldpapers.org/",
CACHE = lru(500),
SUPPORT = express();
// TODO export these from tessera
SUPPORT.use(express.static(path.join(__dirname, "node_modules", "tessera", "public")));
SUPPORT.use(express.static(path.join(__dirname, "node_modules", "tessera", "bower_components")));
var app = express().disable("x-powered-by");
if (process.env.NODE_ENV !== "production") {
app.use(morgan("dev"));
}
app.use(responseTime());
app.use(cors());
if (process.env.SENTRY_DSN) {
var raven = require("raven");
raven.patchGlobal(function(logged, err) {
console.log("Uncaught error. Reporting to Sentry and exiting.");
console.error(err.stack);
process.exit(1);
});
app.use(raven.middleware.express());
}
app.use("/:type(snapshots|atlases)/:slug", function(req, res, next) {
var key = [req.params.type, req.params.slug].join("-"),
route;
if (!(route = CACHE.get(key))) {
route = tessera(tilelive, {
source: util.format("fieldpapers+%s%s/%s", API_BASE_URL, req.params.type, req.params.slug)
});
route.use(SUPPORT);
CACHE.set(key, route);
}
return route(req, res, next);
});
app.use("/[^\/]+/", SUPPORT);
var server = express();
server.use(process.env.PATH_PREFIX || "", app);
server.listen(process.env.PORT || 8080, function() {
console.log("Listening at http://%s:%d/", this.address().address, this.address().port);
});