From 29b3c5f5dc5d8ffca6bd8c3a77277b16aa60d60f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Stra=C3=9Fburger?= Date: Mon, 5 Dec 2016 15:38:01 +0100 Subject: [PATCH] :construction_worker: implementing baseURL to allow usage behind load balancers --- docs/config.rst | 10 ++++++++++ src/serve_data.js | 3 ++- src/serve_rendered.js | 2 +- src/serve_style.js | 5 ++++- src/server.js | 12 ++++++++---- src/utils.js | 12 ++++++++---- 6 files changed, 33 insertions(+), 11 deletions(-) diff --git a/docs/config.rst b/docs/config.rst index 535353d..3a7af5a 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -66,6 +66,16 @@ The value of ``root`` is used as prefix for all data types. You can use this to optionally specify on what domains the rendered tiles are accessible. This can be used for basic load-balancing or to bypass browser's limit for the number of connections per domain. +``baseURL`` +----------- + +When you run the tile server server behind a load-balancer, the automatic URL generators wont't have the required information to build URLs which point to the correct LB endpoint. + +Also, if your load balancer is forwarding a HTTPS connection to the non-HTTPS ``tileserver-gl``, setting the ``baseURL`` is indispensable. + +Not compatible with the ``domains`` option. + + ``formatQuality`` ----------------- diff --git a/src/serve_data.js b/src/serve_data.js index 5a2d236..c86eb4d 100644 --- a/src/serve_data.js +++ b/src/serve_data.js @@ -14,7 +14,8 @@ module.exports = function(options, repo, params, id) { var mbtilesFile = path.resolve(options.paths.mbtiles, params.mbtiles); var tileJSON = { - 'tiles': params.domains || options.domains + 'tiles': params.domains || options.domains, + 'baseURL': options.baseURL }; repo[id] = tileJSON; diff --git a/src/serve_rendered.js b/src/serve_rendered.js index 337b05b..d102424 100644 --- a/src/serve_rendered.js +++ b/src/serve_rendered.js @@ -539,7 +539,7 @@ module.exports = function(options, repo, params, id, dataResolver) { app.get('/rendered.json', function(req, res, next) { var info = clone(tileJSON); info.tiles = utils.getTileUrls(req, info.tiles, - 'styles/' + id + '/rendered', info.format); + 'styles/' + id + '/rendered', info.format, info.baseURL); return res.send(info); }); diff --git a/src/serve_style.js b/src/serve_style.js index 6b5c390..06b3b6d 100644 --- a/src/serve_style.js +++ b/src/serve_style.js @@ -12,6 +12,8 @@ module.exports = function(options, repo, params, id, reportTiles, reportFont) { var styleFile = path.join(options.paths.styles, params.style); + var baseURL = options.baseURL; + var styleJSON = clone(require(styleFile)); Object.keys(styleJSON.sources).forEach(function(name) { var source = styleJSON.sources[name]; @@ -57,8 +59,9 @@ module.exports = function(options, repo, params, id, reportTiles, reportFont) { if (!opt_nokey && req.query.key) { query = '?key=' + req.query.key; } + var url = baseURL ? baseURL : req.protocol + '://' + req.headers.host; return url.replace( - 'local://', req.protocol + '://' + req.headers.host + '/') + query; + 'local://', url + '/') + query; }; var styleJSON_ = clone(styleJSON); diff --git a/src/server.js b/src/server.js index 702f811..50d4062 100644 --- a/src/server.js +++ b/src/server.js @@ -69,6 +69,8 @@ module.exports = function(opts, callback) { } var options = config.options || {}; + var baseURL = config.baseURL; + var paths = options.paths || {}; options.paths = paths; paths.root = path.resolve( @@ -165,7 +167,7 @@ module.exports = function(opts, callback) { version: styleJSON.version, name: styleJSON.name, id: id, - url: req.protocol + '://' + req.headers.host + + url: (baseURL ? baseURL : req.protocol + '://' + req.headers.host) + '/styles/' + id + '.json' + query }); }); @@ -176,7 +178,7 @@ module.exports = function(opts, callback) { Object.keys(serving[type]).forEach(function(id) { var info = clone(serving[type][id]); info.tiles = utils.getTileUrls(req, info.tiles, - type + '/' + id, info.format); + type + '/' + id, info.format, baseURL); arr.push(info); }); return arr; @@ -224,6 +226,7 @@ module.exports = function(opts, callback) { serveTemplate('/$', 'index', function(req) { var styles = clone(config.styles || {}); + Object.keys(styles).forEach(function(id) { var style = styles[id]; style.name = (serving.styles[id] || serving.rendered[id] || {}).name; @@ -247,9 +250,10 @@ module.exports = function(opts, callback) { base64url('http://' + req.headers.host + '/styles/' + id + '/rendered.json' + query) + '/wmts'; + var tiles = utils.getTileUrls( req, style.serving_rendered.tiles, - 'styles/' + id + '/rendered', style.serving_rendered.format); + 'styles/' + id + '/rendered', style.serving_rendered.format, baseURL); style.xyz_link = tiles[0]; } }); @@ -277,7 +281,7 @@ module.exports = function(opts, callback) { '/data/' + id + '.json' + query) + '/wmts'; var tiles = utils.getTileUrls( - req, data_.tiles, 'data/' + id, data_.format); + req, data_.tiles, 'data/' + id, data_.format, baseURL); data_.xyz_link = tiles[0]; } if (data_.filesize) { diff --git a/src/utils.js b/src/utils.js index abc084f..2d0da7b 100644 --- a/src/utils.js +++ b/src/utils.js @@ -6,20 +6,24 @@ var async = require('async'), var glyphCompose = require('glyph-pbf-composite'); -module.exports.getTileUrls = function(req, domains, path, format) { +module.exports.getTileUrls = function(req, domains, path, format, baseURL) { + var key = req.query.key; + var query = (key && key.length > 0) ? ('?key=' + key) : ''; + + if (baseURL) { + return [baseURL + path + '/{z}/{x}/{y}.' + format + query] + } if (domains) { if (domains.constructor === String && domains.length > 0) { domains = domains.split(','); } + } if (!domains || domains.length == 0) { domains = [req.headers.host]; } - var key = req.query.key; - var query = (key && key.length > 0) ? ('?key=' + key) : ''; - var uris = []; domains.forEach(function(domain) { uris.push(req.protocol + '://' + domain + '/' + path +