-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·160 lines (130 loc) · 4.22 KB
/
server.js
File metadata and controls
executable file
·160 lines (130 loc) · 4.22 KB
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
#!/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("node:os").cpus().length * 1.5));
const fs = require("node:fs");
const path = require("node:path");
const cors = require("cors");
const express = require("express");
const morgan = require("morgan");
const responseTime = require("response-time");
const serve = require("./lib/app");
const tessera = require("./lib/index");
const debug = require("debug")("tessera");
module.exports = (opts, callback) => {
const app = express().disable("x-powered-by");
const tilelive = require("tilelive-cache")(require("@mapbox/tilelive"), {
size: process.env.CACHE_SIZE || opts.cacheSize,
sources: process.env.SOURCE_CACHE_SIZE || opts.sourceCacheSize,
});
callback = callback || (() => {});
// 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(serve(tilelive, opts.uri));
tilelive.load(opts.uri, (err, src) => {
if (err) {
throw err;
}
return tessera.getInfo(src, (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")));
}
});
});
}
if (opts.config) {
const configPath = path.resolve(opts.config);
const stats = fs.statSync(configPath);
let config = {};
if (stats.isFile()) {
config = require(configPath);
} else if (stats.isDirectory()) {
config = fs
.readdirSync(configPath)
.filter((filename) => path.extname(filename) === ".json")
.reduce((config, filename) => {
const localConfig = require(path.join(configPath, filename));
return Object.keys(localConfig).reduce((config, k) => {
config[k] = localConfig[k];
return config;
}, config);
}, config);
}
Object.keys(config).forEach((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, serve(tilelive, config[prefix]));
// config[prefix] is a string
let source = config[prefix];
if (source.source != null) {
// actually, it's an object
source = source.source;
}
tilelive.load(source, (err, src) => {
if (err) {
throw err;
}
return tessera.getInfo(src, (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")),
);
}
});
});
});
}
const handler =
process.env.SOCKET || opts.socket || process.env.PORT || opts.port;
const server = app.listen(handler, process.env.HOST || opts.bind, () => {
let 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 {
const addr = server.address();
endpoint = `http://${addr.address}:${addr.port}`;
}
console.log("Listening at %s", endpoint);
return callback();
});
process.on("SIGINT", () => {
console.warn("Caught SIGINT, terminating");
server.close();
process.exit();
});
};