forked from mojodna/tessera
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathserver.js
executable file
·157 lines (126 loc) · 4.69 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
#!/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 async = require("async"),
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("tilelive"), {
size: process.env.CACHE_SIZE || opts.cacheSize,
sources: process.env.SOURCE_CACHE_SIZE || opts.sourceCacheSize
});
app.enable('trust proxy');
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]));
});
// serve index.html even on the root
app.use("/", express.static(path.join(__dirname, "public")));
app.use("/", express.static(path.join(__dirname, "bower_components")));
// aggregate index.json on root for multiple sources
app.get("/index.json", function(req, res, next) {
var queue = [];
Object.keys(config).forEach(function(prefix) {
queue.push(function(callback) {
var url = config[prefix].source || config[prefix];
tilelive.load(url, function(err, source) {
if (err) {
throw err;
}
tessera.getInfo(source, function(err, info) {
if (err) {
throw err;
}
var domains = [],
tilePath = config[prefix].tilePath || "/{z}/{x}/{y}.{format}";
if (config[prefix].domains && config[prefix].domains.length > 0) {
domains = config[prefix].domains.split(',');
}
if (prefix.length > 1) {
info.basename = prefix.substr(1);
}
info.tiles = serve.getTileUrls(domains, req.headers.host, prefix,
tilePath, info.format,
req.query.key, req.protocol);
info.tilejson = "2.0.0";
callback(null, info);
});
});
});
});
return async.parallel(queue, function(err, results) {
return res.send(results);
});
});
}
app.listen(process.env.PORT || opts.port, function() {
console.log("Listening at http://%s:%d/", this.address().address, this.address().port);
return callback();
});
};