forked from mojodna/tessera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·219 lines (178 loc) · 6.58 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
#!/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 fs = require("fs"),
path = require("path");
var cors = require("cors"),
debug = require("debug"),
express = require("express"),
morgan = require("morgan"),
responseTime = require("response-time"),
lru = require("lru-cache"),
handlebars = require("handlebars"),
sqlstring = require("sqlstring"),
jwt = require('express-jwt'),
cookieParser = require('cookie-parser');
var CACHE = lru(50);
var serve = require("./lib/app"),
tessera = require("./lib/index");
debug = debug("tessera");
// Add morgan token 'id' that extracts user email from JWT or 'anon' if not authenticated
morgan.token('id', function getId (req) {
return req.user && req.user.email || 'anon';
});
handlebars.registerHelper('sqlEscape', function(str) {
return new handlebars.SafeString(sqlstring.escape(str));
});
module.exports = function(opts, callback) {
var app = express().disable("x-powered-by"),
tilelive = require("tilelive-cache")(require("@mapbox/tilelive"), {
size: process.env.CACHE_SIZE || opts.cacheSize,
sources: process.env.SOURCE_CACHE_SIZE || opts.sourceCacheSize
});
callback = callback || function() {};
// load and register tilelive modules
require("tilelive-modules/loader")(tilelive, opts);
if (process.env.NODE_ENV === "production") {
app.use(morgan(":remote-addr - :id [:date[clf]] \":method :url HTTP/:http-version\" :status :res[content-length] - :response-time ms"));
}
else {
// TODO configurable logging per-style
app.use(morgan("dev"));
}
if (opts.jwtSecret) {
app.use(cookieParser());
app.use(jwt({
secret: opts.jwtSecret,
getToken: function fromRequest (req) {
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
return req.headers.authorization.split(' ')[1];
} else if (req.query && req.query.token) {
return req.query.token;
} else if (req.cookies && req.cookies.token) {
return req.cookies.token;
}
return null;
}
}));
app.use(function (err, req, res, next) {
if (err.name === 'UnauthorizedError') {
res.status(401).send('Invalid token');
}
});
}
if (opts.uri) {
app.use(responseTime());
app.use(cors());
app.use(express.static(path.join(__dirname, "public")));
app.use(express.static(path.join(__dirname, "bower_components")));
app.use(serve(tilelive, opts.uri));
tilelive.load(opts.uri, function(err, src) {
if (err) {
throw err;
}
return tessera.getInfo(src, function(err, info) {
if (err) {
debug(err.stack);
return;
}
if (info.format === "pbf") {
app.use("/_", serve(tilelive, "xray+" + opts.uri));
app.use("/_", express.static(path.join(__dirname, "public")));
app.use("/_", express.static(path.join(__dirname, "bower_components")));
}
});
});
}
if (opts.config) {
var configPath = path.resolve(opts.config),
stats = fs.statSync(configPath),
config = {};
if (stats.isFile()) {
config = require(configPath);
} else if (stats.isDirectory()) {
config = fs.readdirSync(configPath)
.filter(function(filename) {
return path.extname(filename) === ".json";
})
.reduce(function(config, filename) {
var localConfig = require(path.join(configPath, filename));
return Object.keys(localConfig).reduce(function(config, k) {
config[k] = localConfig[k];
return config;
}, config);
}, config);
}
Object.keys(config).forEach(function(prefix) {
// config[prefix] is a string
var source = config[prefix];
if (source.source) {
// actually, it's an object
source = source.source;
}
var sourceTemplate = handlebars.compile(source);
if (config[prefix].timing !== false) {
app.use(prefix, responseTime());
}
if (config[prefix].cors !== false) {
app.use(prefix, cors());
}
app.use(prefix, express.static(path.join(__dirname, "public")));
app.use(prefix, express.static(path.join(__dirname, "bower_components")));
app.use(prefix, function(req, res, next) {
var key = prefix + ',' + Object.keys(req.query).sort().map(function(key) {return key + '=' + req.query[key];});
var route;
if (!(route = CACHE.get(key))) {
var interpolatedSource = sourceTemplate(Object.assign({}, config[prefix].defaultParams, req.query));
debug('Caching route: ' + key);
debug('Interpolated source:' + interpolatedSource);
var patchedConfig = Object.assign({}, config[prefix], {source: interpolatedSource});
route = serve(tilelive, patchedConfig);
CACHE.set(key, route);
}
return route(req, res, next);
});
// defaultParams are used for xray
var xraySource = sourceTemplate(config[prefix].defaultParams);
tilelive.load(xraySource, function(err, src) {
if (err) {
throw err;
}
return tessera.getInfo(src, function(err, info) {
if (err) {
debug(err.stack);
return;
}
if (info.format === "pbf") {
app.use(prefix + "/_", serve(tilelive, "xray+" + xraySource));
app.use(prefix + "/_", express.static(path.join(__dirname, "public")));
app.use(prefix + "/_", express.static(path.join(__dirname, "bower_components")));
}
});
});
});
}
var handler = process.env.SOCKET || opts.socket || process.env.PORT || opts.port;
var server = app.listen(handler, process.env.HOST || opts.bind, function() {
var endpoint;
if (opts.socket) {
endpoint = opts.socket;
// allow the socket to be accessed by other users/groups
fs.chmodSync(opts.socket, "1766");
} else if (process.env.SOCKET) {
endpoint = process.env.SOCKET;
// allow the socket to be accessed by other users/groups
fs.chmodSync(opts.socket, "1766");
} else {
endpoint = "http://" + this.address().address + ':' + this.address().port;
}
console.log("Listening at %s", endpoint);
return callback();
});
process.on('SIGINT', function () {
console.warn('Caught SIGINT, terminating');
server.close();
process.exit();
});
};