Major refactoring of the urls (#5)
This commit is contained in:
parent
6f644a4c03
commit
4c40700bac
6 changed files with 180 additions and 94 deletions
|
@ -23,7 +23,7 @@
|
|||
<script>
|
||||
tileserver({
|
||||
index: "index.json" + location.search,
|
||||
tilejson: "%n.json" + location.search
|
||||
tilejson: "raster/%n.json" + location.search
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
|
|
@ -31,18 +31,15 @@ mbgl.on('message', function(e) {
|
|||
}
|
||||
});
|
||||
|
||||
module.exports = function(maps, options, prefix) {
|
||||
var app = express().disable('x-powered-by'),
|
||||
domains = options.domains,
|
||||
tilePath = '/{z}/{x}/{y}.{format}';
|
||||
module.exports = function(repo, options, id) {
|
||||
var app = express().disable('x-powered-by');
|
||||
|
||||
var rootPath = path.join(process.cwd(), options.root || '');
|
||||
|
||||
var styleUrl = options.style;
|
||||
var map = {
|
||||
renderers: [],
|
||||
sources: {},
|
||||
tileJSON: {}
|
||||
sources: {}
|
||||
};
|
||||
|
||||
var styleJSON;
|
||||
|
@ -130,17 +127,18 @@ module.exports = function(maps, options, prefix) {
|
|||
|
||||
styleJSON = require(path.join(rootPath, styleUrl));
|
||||
|
||||
map.tileJSON = {
|
||||
var tileJSON = {
|
||||
'tilejson': '2.0.0',
|
||||
'name': styleJSON.name,
|
||||
'basename': prefix.substr(1),
|
||||
'basename': id,
|
||||
'minzoom': 0,
|
||||
'maxzoom': 20,
|
||||
'bounds': [-180, -85.0511, 180, 85.0511],
|
||||
'format': 'png',
|
||||
'type': 'baselayer'
|
||||
'type': 'baselayer',
|
||||
'tiles': options.domains
|
||||
};
|
||||
Object.assign(map.tileJSON, options.options || {});
|
||||
Object.assign(tileJSON, options.tilejson || {});
|
||||
|
||||
var queue = [];
|
||||
Object.keys(styleJSON.sources).forEach(function(name) {
|
||||
|
@ -158,7 +156,7 @@ module.exports = function(maps, options, prefix) {
|
|||
source.basename = name;
|
||||
source.tiles = [
|
||||
// meta url which will be detected when requested
|
||||
'mbtiles://' + name + tilePath.replace('{format}', 'pbf')
|
||||
'mbtiles://' + name + '/{z}/{x}/{y}.pbf'
|
||||
];
|
||||
callback(null);
|
||||
});
|
||||
|
@ -174,15 +172,10 @@ module.exports = function(maps, options, prefix) {
|
|||
map.renderers[3] = createPool(3, 2, 4);
|
||||
});
|
||||
|
||||
maps[prefix] = map;
|
||||
repo[id] = tileJSON;
|
||||
|
||||
var tilePattern = tilePath
|
||||
.replace(/\.(?!.*\.)/, ':scale(' + SCALE_PATTERN + ')?.')
|
||||
.replace(/\./g, '\.')
|
||||
.replace('{z}', ':z(\\d+)')
|
||||
.replace('{x}', ':x(\\d+)')
|
||||
.replace('{y}', ':y(\\d+)')
|
||||
.replace('{format}', ':format([\\w]+)');
|
||||
var tilePattern = '/raster/' + id + '/:z(\\d+)/:x(\\d+)/:y(\\d+)' +
|
||||
':scale(' + SCALE_PATTERN + ')?\.:format([\\w]+)';
|
||||
|
||||
var respondImage = function(z, lon, lat, width, height, scale, format, res, next) {
|
||||
if (Math.abs(lon) > 180 || Math.abs(lat) > 85.06) {
|
||||
|
@ -267,7 +260,7 @@ module.exports = function(maps, options, prefix) {
|
|||
});
|
||||
|
||||
var staticPattern =
|
||||
'/static/%s:scale(' + SCALE_PATTERN + ')?\.:format([\\w]+)';
|
||||
'/static/' + id + '/%s:scale(' + SCALE_PATTERN + ')?\.:format([\\w]+)';
|
||||
|
||||
var centerPattern =
|
||||
util.format(':lon(%s),:lat(%s),:z(\\d+)/:width(\\d+)x:height(\\d+)',
|
||||
|
@ -303,5 +296,12 @@ module.exports = function(maps, options, prefix) {
|
|||
return respondImage(z, x, y, w, h, scale, format, res, next);
|
||||
});
|
||||
|
||||
app.get('/raster/' + id + '.json', function(req, res, next) {
|
||||
var info = clone(tileJSON);
|
||||
info.tiles = utils.getTileUrls(req, info.tiles,
|
||||
'raster/' + id, info.format);
|
||||
return res.send(info);
|
||||
});
|
||||
|
||||
return app;
|
||||
};
|
||||
|
|
48
src/serve_style.js
Normal file
48
src/serve_style.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
'use strict';
|
||||
|
||||
var path = require('path');
|
||||
|
||||
var clone = require('clone'),
|
||||
express = require('express');
|
||||
|
||||
|
||||
module.exports = function(repo, options, id, reportVector) {
|
||||
var app = express().disable('x-powered-by');
|
||||
|
||||
var rootPath = path.join(process.cwd(), options.root || '');
|
||||
|
||||
var styleUrl = path.join(rootPath, options.style);
|
||||
|
||||
var styleJSON = clone(require(styleUrl));
|
||||
Object.keys(styleJSON.sources).forEach(function(name) {
|
||||
var source = styleJSON.sources[name];
|
||||
var url = source.url;
|
||||
if (url.lastIndexOf('mbtiles:', 0) === 0) {
|
||||
var mbtiles = url.substring('mbtiles://'.length);
|
||||
var identifier = reportVector(mbtiles);
|
||||
source.url = 'local://vector/' + identifier + '.json';
|
||||
}
|
||||
});
|
||||
styleJSON.sprite = 'local://styles/{style_id}/sprite';
|
||||
styleJSON.glyphs = 'local://fonts/{fonstack}/{range}.pbf';
|
||||
|
||||
repo[id] = styleJSON;
|
||||
|
||||
app.get('/styles/' + id + '.json', function(req, res, next) {
|
||||
var fixUrl = function(url) {
|
||||
return url.replace(
|
||||
'local://', req.protocol + '://' + req.headers.host + '/');
|
||||
};
|
||||
|
||||
var styleJSON_ = clone(styleJSON);
|
||||
Object.keys(styleJSON_.sources).forEach(function(name) {
|
||||
var source = styleJSON_.sources[name];
|
||||
source.url = fixUrl(source.url);
|
||||
});
|
||||
styleJSON_.sprite = fixUrl(styleJSON_.sprite);
|
||||
styleJSON_.glyphs = fixUrl(styleJSON_.glyphs);
|
||||
return res.send(styleJSON_);
|
||||
});
|
||||
|
||||
return app;
|
||||
};
|
|
@ -9,44 +9,40 @@ var clone = require('clone'),
|
|||
|
||||
var utils = require('./utils');
|
||||
|
||||
module.exports = function(maps, options, prefix) {
|
||||
var app = express().disable('x-powered-by'),
|
||||
domains = options.domains,
|
||||
tilePath = '/{z}/{x}/{y}.pbf';
|
||||
module.exports = function(repo, options, id) {
|
||||
var app = express().disable('x-powered-by');
|
||||
|
||||
var rootPath = path.join(process.cwd(), options.root || '');
|
||||
|
||||
var mbtilesPath = options.mbtiles;
|
||||
var map = {
|
||||
tileJSON: {}
|
||||
var tileJSON = {
|
||||
'tiles': options.domains
|
||||
};
|
||||
maps[prefix] = map;
|
||||
|
||||
repo[id] = tileJSON;
|
||||
|
||||
var source = new mbtiles(path.join(rootPath, mbtilesPath), function(err) {
|
||||
source.getInfo(function(err, info) {
|
||||
map.tileJSON['name'] = prefix.substr(1);
|
||||
tileJSON['name'] = id;
|
||||
|
||||
Object.assign(map.tileJSON, info);
|
||||
Object.assign(tileJSON, info);
|
||||
|
||||
map.tileJSON['tilejson'] = '2.0.0';
|
||||
map.tileJSON['basename'] = prefix.substr(1);
|
||||
map.tileJSON['format'] = 'pbf';
|
||||
tileJSON['tilejson'] = '2.0.0';
|
||||
tileJSON['basename'] = id;
|
||||
tileJSON['format'] = 'pbf';
|
||||
|
||||
Object.assign(map.tileJSON, options.options || {});
|
||||
Object.assign(tileJSON, options.tilejson || {});
|
||||
});
|
||||
});
|
||||
|
||||
var tilePattern = tilePath
|
||||
.replace('{z}', ':z(\\d+)')
|
||||
.replace('{x}', ':x(\\d+)')
|
||||
.replace('{y}', ':y(\\d+)');
|
||||
var tilePattern = '/vector/' + id + '/:z(\\d+)/:x(\\d+)/:y(\\d+).pbf';
|
||||
|
||||
app.get(tilePattern, function(req, res, next) {
|
||||
var z = req.params.z | 0,
|
||||
x = req.params.x | 0,
|
||||
y = req.params.y | 0;
|
||||
if (z < map.tileJSON.minzoom || 0 || x < 0 || y < 0 ||
|
||||
z > map.tileJSON.maxzoom ||
|
||||
if (z < tileJSON.minzoom || 0 || x < 0 || y < 0 ||
|
||||
z > tileJSON.maxzoom ||
|
||||
x >= Math.pow(2, z) || y >= Math.pow(2, z)) {
|
||||
return res.status(404).send('Out of bounds');
|
||||
}
|
||||
|
@ -73,5 +69,12 @@ module.exports = function(maps, options, prefix) {
|
|||
});
|
||||
});
|
||||
|
||||
app.get('/vector/' + id + '.json', function(req, res, next) {
|
||||
var info = clone(tileJSON);
|
||||
info.tiles = utils.getTileUrls(req, info.tiles,
|
||||
'vector/' + id, info.format);
|
||||
return res.send(info);
|
||||
});
|
||||
|
||||
return app;
|
||||
};
|
||||
|
|
129
src/server.js
129
src/server.js
|
@ -7,19 +7,23 @@ process.env.UV_THREADPOOL_SIZE =
|
|||
var fs = require('fs'),
|
||||
path = require('path');
|
||||
|
||||
var async = require('async'),
|
||||
clone = require('clone'),
|
||||
var clone = require('clone'),
|
||||
cors = require('cors'),
|
||||
express = require('express'),
|
||||
morgan = require('morgan');
|
||||
|
||||
var serve_raster = require('./serve_raster'),
|
||||
serve_style = require('./serve_style'),
|
||||
serve_vector = require('./serve_vector'),
|
||||
utils = require('./utils');
|
||||
|
||||
module.exports = function(opts, callback) {
|
||||
var app = express().disable('x-powered-by'),
|
||||
maps = {};
|
||||
serving = {
|
||||
styles: {},
|
||||
raster: {},
|
||||
vector: {}
|
||||
};
|
||||
|
||||
app.enable('trust proxy');
|
||||
|
||||
|
@ -33,55 +37,90 @@ module.exports = function(opts, callback) {
|
|||
var configPath = path.resolve(opts.config),
|
||||
config = require(configPath);
|
||||
|
||||
Object.keys(config).forEach(function(prefix) {
|
||||
if (config[prefix].cors !== false) {
|
||||
app.use(prefix, cors());
|
||||
var vector = clone(config.vector);
|
||||
|
||||
Object.keys(config.styles || {}).forEach(function(id) {
|
||||
var item = config.styles[id];
|
||||
if (!item.style || item.style.length == 0) {
|
||||
console.log('Missing "style" property for ' + id);
|
||||
return;
|
||||
}
|
||||
|
||||
if (config[prefix].style) {
|
||||
app.use(prefix, serve_raster(maps, config[prefix], prefix));
|
||||
} else {
|
||||
app.use(prefix, serve_vector(maps, config[prefix], prefix));
|
||||
if (item.vector !== false) {
|
||||
app.use('/', serve_style(serving.styles, item, id,
|
||||
function(mbtiles) {
|
||||
var vectorItemId;
|
||||
Object.keys(vector).forEach(function(id) {
|
||||
if (vector[id].mbtiles == mbtiles) {
|
||||
vectorItemId = id;
|
||||
}
|
||||
});
|
||||
if (vectorItemId) { // mbtiles exist in the vector config
|
||||
return vectorItemId;
|
||||
} else {
|
||||
var id = mbtiles.substr(0, mbtiles.lastIndexOf('.')) || mbtiles;
|
||||
while (vector[id]) id += '_';
|
||||
vector[id] = {
|
||||
'mbtiles': mbtiles
|
||||
};
|
||||
return id;
|
||||
}
|
||||
}));
|
||||
}
|
||||
if (item.raster !== false) {
|
||||
app.use('/', serve_raster(serving.raster, item, id));
|
||||
}
|
||||
});
|
||||
|
||||
// serve index.html on the root
|
||||
//TODO: cors
|
||||
|
||||
Object.keys(vector).forEach(function(id) {
|
||||
var item = vector[id];
|
||||
if (!item.mbtiles || item.mbtiles.length == 0) {
|
||||
console.log('Missing "mbtiles" property for ' + id);
|
||||
return;
|
||||
}
|
||||
|
||||
app.use('/', serve_vector(serving.vector, item, id));
|
||||
});
|
||||
|
||||
app.get('/styles.json', function(req, res, next) {
|
||||
var result = [];
|
||||
Object.keys(serving.styles).forEach(function(id) {
|
||||
var styleJSON = serving.styles[id];
|
||||
result.push({
|
||||
version: styleJSON.version,
|
||||
name: styleJSON.name,
|
||||
id: id,
|
||||
url: req.protocol + '://' + req.headers.host + '/styles/' + id + '.json'
|
||||
});
|
||||
});
|
||||
res.send(result);
|
||||
});
|
||||
|
||||
var addTileJSONs = function(arr, req, type) {
|
||||
Object.keys(serving[type]).forEach(function(id) {
|
||||
var info = clone(serving[type][id]);
|
||||
info.tiles = utils.getTileUrls(req, info.tiles,
|
||||
type + '/' + id, info.format);
|
||||
arr.push(info);
|
||||
});
|
||||
return arr;
|
||||
};
|
||||
|
||||
app.get('/raster.json', function(req, res, next) {
|
||||
res.send(addTileJSONs([], req, 'raster'));
|
||||
});
|
||||
app.get('/vector.json', function(req, res, next) {
|
||||
res.send(addTileJSONs([], req, 'vector'));
|
||||
});
|
||||
app.get('/index.json', function(req, res, next) {
|
||||
res.send(addTileJSONs(addTileJSONs([], req, 'raster'), req, 'vector'));
|
||||
});
|
||||
|
||||
// serve viewer on the root
|
||||
app.use('/', express.static(path.join(__dirname, '../public')));
|
||||
|
||||
app.get(/^(\/[^\/]+)\.json$/, function(req, res, next) {
|
||||
var prefix = req.params[0];
|
||||
if (prefix == '/index') {
|
||||
var queue = [];
|
||||
Object.keys(config).forEach(function(mapPrefix) {
|
||||
var map = maps[mapPrefix];
|
||||
queue.push(function(callback) {
|
||||
var info = clone(map.tileJSON);
|
||||
|
||||
info.tiles = utils.getTileUrls(
|
||||
req.protocol, config[mapPrefix].domains, req.headers.host,
|
||||
mapPrefix, '/{z}/{x}/{y}.{format}', info.format, req.query.key);
|
||||
|
||||
callback(null, info);
|
||||
});
|
||||
});
|
||||
return async.parallel(queue, function(err, results) {
|
||||
return res.send(results);
|
||||
});
|
||||
} else {
|
||||
var map = maps[prefix];
|
||||
if (!map || !map.tileJSON) {
|
||||
return res.status(404).send('Not found');
|
||||
}
|
||||
var info = clone(map.tileJSON);
|
||||
|
||||
info.tiles = utils.getTileUrls(
|
||||
req.protocol, config[prefix].domains, req.headers.host,
|
||||
prefix, '/{z}/{x}/{y}.{format}', info.format, req.query.key);
|
||||
|
||||
return res.send(info);
|
||||
}
|
||||
});
|
||||
|
||||
var server = app.listen(process.env.PORT || opts.port, function() {
|
||||
console.log('Listening at http://%s:%d/',
|
||||
this.address().address, this.address().port);
|
||||
|
|
14
src/utils.js
14
src/utils.js
|
@ -1,7 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
module.exports.getTileUrls = function(
|
||||
protocol, domains, host, path, tilePath, format, key) {
|
||||
module.exports.getTileUrls = function(req, domains, path, format) {
|
||||
|
||||
if (domains) {
|
||||
if (domains.constructor === String && domains.length > 0) {
|
||||
|
@ -9,19 +8,16 @@ module.exports.getTileUrls = function(
|
|||
}
|
||||
}
|
||||
if (!domains || domains.length == 0) {
|
||||
domains = [host];
|
||||
domains = [req.headers.host];
|
||||
}
|
||||
|
||||
var key = req.query.key;
|
||||
var query = (key && key.length > 0) ? ('?key=' + key) : '';
|
||||
if (path == '/') {
|
||||
path = '';
|
||||
}
|
||||
|
||||
var uris = [];
|
||||
domains.forEach(function(domain) {
|
||||
uris.push(protocol + '://' + domain + path +
|
||||
tilePath.replace('{format}', format).replace(/\/+/g, '/') +
|
||||
query);
|
||||
uris.push(req.protocol + '://' + domain + '/' + path +
|
||||
'/{z}/{x}/{y}.' + format + query);
|
||||
});
|
||||
|
||||
return uris;
|
||||
|
|
Loading…
Reference in a new issue