From 0bf2e08b27c45649d56d5b5930e2f94db914281c Mon Sep 17 00:00:00 2001 From: Ilya Kreymer Date: Fri, 24 Aug 2018 17:59:02 -0700 Subject: [PATCH] non-root deployment and static prefix: (ported from uk-pywb fork) (#373) - store original wsgi SCRIPT_NAME (before collection path is pushed) in 'pywb.app_prefix' env var - set 'pywb.host_prefix' via rewriterapp - add 'static_prefix' jinja env global which defaults to 'pywb.host_prefix + pywb.app_prefix + static/' - set 'static_prefix' to absolute url if available (to support proxy mode) - update existing templates to use '{{ static_prefix }}' instead of '{{ host_prefix }}/{{ static_path }' - update index.html to use pywb.app_prefix for collection links - tests: add test_prefixed_deploy.py to ensure all paths are prefixed as expected --- pywb/apps/frontendapp.py | 3 ++ pywb/apps/rewriterapp.py | 2 ++ pywb/rewrite/templateview.py | 3 ++ pywb/templates/banner.html | 4 +-- pywb/templates/frame_insert.html | 2 +- pywb/templates/head_insert.html | 6 ++-- pywb/templates/index.html | 2 +- pywb/templates/query.html | 12 ++++---- tests/test_prefixed_deploy.py | 50 ++++++++++++++++++++++++++++++++ 9 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 tests/test_prefixed_deploy.py diff --git a/pywb/apps/frontendapp.py b/pywb/apps/frontendapp.py index 3b0b46dc..e740aa1a 100644 --- a/pywb/apps/frontendapp.py +++ b/pywb/apps/frontendapp.py @@ -357,6 +357,9 @@ class FrontEndApp(object): try: endpoint, args = urls.match() + # store original script_name (original prefix) before modifications are made + environ['pywb.app_prefix'] = environ.get('SCRIPT_NAME') + response = endpoint(environ, **args) return response(environ, start_response) diff --git a/pywb/apps/rewriterapp.py b/pywb/apps/rewriterapp.py index 13397561..b467c804 100644 --- a/pywb/apps/rewriterapp.py +++ b/pywb/apps/rewriterapp.py @@ -256,6 +256,8 @@ class RewriterApp(object): urlkey = canonicalize(wb_url.url) + environ['pywb.host_prefix'] = host_prefix + if self.use_js_obj_proxy: content_rw = self.js_proxy_rw else: diff --git a/pywb/rewrite/templateview.py b/pywb/rewrite/templateview.py index 74b1c9c0..c972bb87 100644 --- a/pywb/rewrite/templateview.py +++ b/pywb/rewrite/templateview.py @@ -139,7 +139,10 @@ class BaseInsertView(object): params = env.get(self.jenv.env_template_params_key) if params: kwargs.update(params) + kwargs['env'] = env + kwargs['static_prefix'] = env.get('pywb.host_prefix', '') + env.get('pywb.app_prefix', '') + '/static' + return template.render(**kwargs) diff --git a/pywb/templates/banner.html b/pywb/templates/banner.html index dffe1057..d240b9da 100644 --- a/pywb/templates/banner.html +++ b/pywb/templates/banner.html @@ -1,5 +1,5 @@ {% if not env.pywb_proxy_magic or config.proxy.use_banner | default(true) %} - - + + {% endif %} diff --git a/pywb/templates/frame_insert.html b/pywb/templates/frame_insert.html index b8fbe1bb..1f7e74e1 100644 --- a/pywb/templates/frame_insert.html +++ b/pywb/templates/frame_insert.html @@ -12,7 +12,7 @@ html, body } - + {{ banner_html }} diff --git a/pywb/templates/head_insert.html b/pywb/templates/head_insert.html index 78a198cd..3eb7fe45 100644 --- a/pywb/templates/head_insert.html +++ b/pywb/templates/head_insert.html @@ -23,11 +23,11 @@ wbinfo.is_live = {{ is_live }}; wbinfo.coll = "{{ coll }}"; wbinfo.proxy_magic = "{{ env.pywb_proxy_magic }}"; - wbinfo.static_prefix = "{{ host_prefix }}/{{ static_path }}/"; + wbinfo.static_prefix = "{{ static_prefix }}/"; {% if not wb_url.is_banner_only %} - + + {% endif %} {{ banner_html }} diff --git a/pywb/templates/index.html b/pywb/templates/index.html index ae1456ce..641d4701 100644 --- a/pywb/templates/index.html +++ b/pywb/templates/index.html @@ -8,7 +8,7 @@ This archive contains the following collections: