-
Notifications
You must be signed in to change notification settings - Fork 80
/
server.js
executable file
·156 lines (126 loc) · 4.5 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
#!/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");
var serve = require("./lib/app"),
tessera = require("./lib/index");
debug = debug("tessera");
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") {
// 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) {
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) {
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]));
// config[prefix] is a string
var source = config[prefix];
if (source.source != null) {
// actually, it's an object
source = source.source;
}
tilelive.load(source, 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+" + config[prefix].source));
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://" + server.address().address + ':' + server.address().port;
}
console.log("Listening at %s", endpoint);
return callback();
});
process.on('SIGINT', function () {
console.warn('Caught SIGINT, terminating');
server.close();
process.exit();
});
};