diff options
-rw-r--r-- | fietsboek/__init__.py | 2 | ||||
-rw-r--r-- | fietsboek/jinja2.py | 16 | ||||
-rw-r--r-- | fietsboek/views/tileproxy.py | 9 |
3 files changed, 20 insertions, 7 deletions
diff --git a/fietsboek/__init__.py b/fietsboek/__init__.py index 7aede7b..4835480 100644 --- a/fietsboek/__init__.py +++ b/fietsboek/__init__.py @@ -72,6 +72,8 @@ def main(global_config, **settings): settings.get('available_locales', 'en')) settings['fietsboek.pages'] = aslist( settings.get('fietsboek.pages', '')) + settings['fietsboek.tile_proxy.disable'] = asbool( + settings.get('fietsboek.tile_proxy.disable', 'false')) settings['thunderforest.maps'] = aslist( settings.get('thunderforest.maps', '')) settings['fietsboek.default_tile_layers'] = aslist( diff --git a/fietsboek/jinja2.py b/fietsboek/jinja2.py index ff60660..a0b9457 100644 --- a/fietsboek/jinja2.py +++ b/fietsboek/jinja2.py @@ -94,13 +94,21 @@ def global_embed_tile_layers(request): # pylint: disable=import-outside-toplevel,cyclic-import from .views import tileproxy tile_sources = tileproxy.sources_for(request) + + if request.registry.settings.get("fietsboek.tile_proxy.disable"): + def _url(source): + return source.url_template + else: + def _url(source): + return (request.route_url("tile-proxy", provider=source.key, x="{x}", y="{y}", z="{z}") + .replace("%7Bx%7D", "{x}") + .replace("%7By%7D", "{y}") + .replace("%7Bz%7D", "{z}")) + return Markup(json.dumps([ { "name": source.name, - "url": request.route_url("tile-proxy", provider=source.key, x="{x}", y="{y}", z="{z}") - .replace("%7Bx%7D", "{x}") - .replace("%7By%7D", "{y}") - .replace("%7Bz%7D", "{z}"), + "url": _url(source), "attribution": source.attribution, "type": source.layer_type.value, } diff --git a/fietsboek/views/tileproxy.py b/fietsboek/views/tileproxy.py index 2a1e6d0..b9a32c4 100644 --- a/fietsboek/views/tileproxy.py +++ b/fietsboek/views/tileproxy.py @@ -193,10 +193,13 @@ def tile_proxy(request): :return: The HTTP response. :rtype: pyramid.response.Response """ + if request.registry.settings.get("fietsboek.tile_proxy.disable"): + raise HTTPBadRequest("Tile proxying is disabled") + provider = request.matchdict['provider'] tile_sources = {source.key: source for source in sources_for(request)} if provider not in tile_sources: - return HTTPBadRequest("Invalid provider") + raise HTTPBadRequest("Invalid provider") x, y, z = (int(request.matchdict['x']), int(request.matchdict['y']), int(request.matchdict['z'])) @@ -212,7 +215,7 @@ def tile_proxy(request): # We've gotten too many timeouts from this provider recently, so avoid # contacting it in the first place. LOGGER.debug("Aborted attempt to contact %s due to previous timeouts", provider) - return HTTPGatewayTimeout(f"Avoiding request to {provider}") + raise HTTPGatewayTimeout(f"Avoiding request to {provider}") url = tile_sources[provider].url_template.format(x=x, y=y, z=z, s=random.choice("abc")) headers = { @@ -228,7 +231,7 @@ def tile_proxy(request): LOGGER.debug("Proxy timeout when accessing %r", url) request.redis.incr(timeout_tracker) request.redis.expire(timeout_tracker, PUNISHMENT_TTL) - return HTTPGatewayTimeout(f"No response in time from {provider}") + raise HTTPGatewayTimeout(f"No response in time from {provider}") from None else: try: resp.raise_for_status() |