Serve TileJSONs on /{prefix}.json

This commit is contained in:
Petr Sloup 2016-03-09 19:18:59 +01:00
parent 9efa22b52b
commit 6f644a4c03
5 changed files with 33 additions and 39 deletions

View file

@ -23,7 +23,7 @@
<script>
tileserver({
index: "index.json" + location.search,
tilejson: "%n/index.json" + location.search
tilejson: "%n.json" + location.search
});
</script>
</body>

View file

@ -303,15 +303,5 @@ module.exports = function(maps, options, prefix) {
return respondImage(z, x, y, w, h, scale, format, res, next);
});
app.get('/index.json', function(req, res, next) {
var info = clone(map.tileJSON);
info.tiles = utils.getTileUrls(req.protocol, domains, req.headers.host,
prefix, tilePath, info.format,
req.query.key);
return res.send(info);
});
return app;
};

View file

@ -73,15 +73,5 @@ module.exports = function(maps, options, prefix) {
});
});
app.get('/index.json', function(req, res, next) {
var info = clone(map.tileJSON);
info.tiles = utils.getTileUrls(req.protocol, domains, req.headers.host,
prefix, tilePath, info.format,
req.query.key);
return res.send(info);
});
return app;
};

View file

@ -48,17 +48,18 @@ module.exports = function(opts, callback) {
// serve index.html on the root
app.use('/', express.static(path.join(__dirname, '../public')));
// aggregate index.json on root for multiple sources
app.get('/index.json', function(req, res, next) {
app.get(/^(\/[^\/]+)\.json$/, function(req, res, next) {
var prefix = req.params[0];
if (prefix == '/index') {
var queue = [];
Object.keys(config).forEach(function(prefix) {
var map = maps[prefix];
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[prefix].domains, req.headers.host,
prefix, '/{z}/{x}/{y}.{format}', info.format, req.query.key);
req.protocol, config[mapPrefix].domains, req.headers.host,
mapPrefix, '/{z}/{x}/{y}.{format}', info.format, req.query.key);
callback(null, info);
});
@ -66,6 +67,19 @@ module.exports = function(opts, callback) {
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() {

View file

@ -17,17 +17,17 @@ describe('Metadata', function() {
});
});
describe('/test/index.json', function() {
describe('/test.json', function() {
it('is json', function(done) {
supertest(app)
.get('/test/index.json')
.get('/test.json')
.expect(200)
.expect('Content-Type', /application\/json/, done);
});
it('has valid basename and tiles', function(done) {
supertest(app)
.get('/test/index.json')
.get('/test.json')
.expect(function(res) {
res.body.basename.should.equal('test');
res.body.tiles.length.should.be.greaterThan(0);