From d98c1f6cf7d02168c36ab0ec0f0c3d3a924fe6d9 Mon Sep 17 00:00:00 2001 From: Ilya Kreymer Date: Wed, 4 Nov 2015 15:36:44 -0800 Subject: [PATCH] memento/api: add a new /collinfo.json end-point, enabled with 'enable_coll_info' config setting, which returns the value fo collinfo.json template. Default template returns an entry for each handler route, including the route path (id), title (name) and memento timegate and timemap paths, to be used with an aggregator. Using a custom 'info_json' template can specify a different collinfo template, alternative to #69 (local aggregation) Closes #146 --- pywb/default_config.yaml | 2 ++ pywb/framework/archivalrouter.py | 14 ++++++++++++-- pywb/templates/collinfo.json | 15 +++++++++++++++ pywb/webapp/pywb_init.py | 1 + tests/test_config.yaml | 4 ++++ tests/test_integration.py | 8 +++++++- 6 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 pywb/templates/collinfo.json diff --git a/pywb/default_config.yaml b/pywb/default_config.yaml index 1c77f1ca..d8fa5f45 100644 --- a/pywb/default_config.yaml +++ b/pywb/default_config.yaml @@ -23,6 +23,7 @@ paths: proxy_cert_download_html: proxy_cert_download.html proxy_select_html: proxy_select.html + info_json: collinfo.json templates_dirs: - templates @@ -49,6 +50,7 @@ not_found_html: not_found.html proxy_cert_download_html: proxy_cert_download.html proxy_select_html: proxy_select.html +info_json: collinfo.json static_default_prefix: &static_default_prefix static/__pywb static_shared_prefix: static/__shared diff --git a/pywb/framework/archivalrouter.py b/pywb/framework/archivalrouter.py index b15f660e..487fa596 100644 --- a/pywb/framework/archivalrouter.py +++ b/pywb/framework/archivalrouter.py @@ -23,9 +23,12 @@ class ArchivalRouter(object): self.home_view = kwargs.get('home_view') self.error_view = kwargs.get('error_view') + self.info_view = kwargs.get('info_view') - self.urlrewriter_class = (kwargs.get('config', {}). - get('urlrewriter_class', UrlRewriter)) + config = kwargs.get('config', {}) + self.urlrewriter_class = config.get('urlrewriter_class', UrlRewriter) + + self.enable_coll_info = config.get('enable_coll_info', False) def __call__(self, env): request_uri = self.ensure_rel_uri_set(env) @@ -43,6 +46,13 @@ class ArchivalRouter(object): if request_uri in ['/', '/index.html', '/index.htm']: return self.render_home_page(env) + if self.enable_coll_info and request_uri in ['/collinfo.json']: + params = env.get('pywb.template_params', {}) + host = WbRequest.make_host_prefix(env) + return self.info_view.render_response(env=env, host=host, routes=self.routes, + content_type='application/json', + **params) + return self.fallback(env, self) if self.fallback else None def parse_request(self, route, env, matcher, coll, request_uri, diff --git a/pywb/templates/collinfo.json b/pywb/templates/collinfo.json new file mode 100644 index 00000000..35cf6986 --- /dev/null +++ b/pywb/templates/collinfo.json @@ -0,0 +1,15 @@ +[ +{% for route in routes %} +{% if route | is_wb_handler %} +{{ ',' if notfirst else '' }} +{ + "id": "{{ route.path }}", + "name": "{{ route.user_metadata.title if route.user_metadata.title else route.path }}", + "timegate": "{{ host }}/{{route.path}}/", + "timemap": "{{ host }}/{{route.path}}/timemap/*/" + +} +{% set notfirst = true %} +{% endif %} +{% endfor %} +] diff --git a/pywb/webapp/pywb_init.py b/pywb/webapp/pywb_init.py index 99154700..4a8c7cca 100644 --- a/pywb/webapp/pywb_init.py +++ b/pywb/webapp/pywb_init.py @@ -381,5 +381,6 @@ def create_wb_router(passed_config=None): abs_path=config.get('absolute_paths', True), home_view=init_view(config, 'home_html'), error_view=init_view(config, 'error_html'), + info_view=init_view(config, 'info_json'), config=config ) diff --git a/tests/test_config.yaml b/tests/test_config.yaml index 8ec0e4bc..6df347f1 100644 --- a/tests/test_config.yaml +++ b/tests/test_config.yaml @@ -121,6 +121,10 @@ proxy_options: use_client_rewrite: true use_wombat: true + +#enable coll info JSON +enable_coll_info: true + # enable cdx server api for querying cdx directly (experimental) #enable_cdx_api: True # or specify suffix diff --git a/tests/test_integration.py b/tests/test_integration.py index 66930d96..80856b02 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -477,7 +477,13 @@ class TestWbIntegration(BaseIntegration): assert resp.status_int == 400 assert 'Invalid Url: http://?abc' in resp.body - #def test_invalid_config(self): + + def test_coll_info_json(self): + resp = self.testapp.get('/collinfo.json') + assert resp.content_type == 'application/json' + assert len(resp.json) == 9 + + #def test_invalid_config(self): # with raises(IOError): # init_app(create_wb_router, # load_yaml=True,