forked from mojodna/tessera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·83 lines (65 loc) · 2.4 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
#!/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");
var cors = require("cors"),
express = require("express"),
morgan = require("morgan"),
responseTime = require("response-time");
var serve = require("./lib/app"),
tessera = require("./lib/index");
module.exports = function(opts, callback) {
var app = express().disable("x-powered-by"),
tilelive = require("tilelive-cache")(require("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("./modules")(tilelive, opts);
if (process.env.NODE_ENV !== "production") {
// TODO configurable logging per-style
app.use(morgan('dev'));
}
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) {
throw err;
}
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 config = require(path.resolve(opts.config));
Object.keys(config).forEach(function(prefix) {
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, serve(tilelive, config[prefix]));
});
}
app.listen(process.env.PORT || opts.port, function() {
console.log("Listening at http://%s:%d/", this.address().address, this.address().port);
return callback();
});
};