Skip to content

Commit d98c1f6

Browse files
committed
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
1 parent dc74b14 commit d98c1f6

File tree

6 files changed

+41
-3
lines changed

6 files changed

+41
-3
lines changed

pywb/default_config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ paths:
2323
proxy_cert_download_html: proxy_cert_download.html
2424
proxy_select_html: proxy_select.html
2525

26+
info_json: collinfo.json
2627

2728
templates_dirs:
2829
- templates
@@ -49,6 +50,7 @@ not_found_html: not_found.html
4950
proxy_cert_download_html: proxy_cert_download.html
5051
proxy_select_html: proxy_select.html
5152

53+
info_json: collinfo.json
5254

5355
static_default_prefix: &static_default_prefix static/__pywb
5456
static_shared_prefix: static/__shared

pywb/framework/archivalrouter.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ def __init__(self, routes, **kwargs):
2323

2424
self.home_view = kwargs.get('home_view')
2525
self.error_view = kwargs.get('error_view')
26+
self.info_view = kwargs.get('info_view')
2627

27-
self.urlrewriter_class = (kwargs.get('config', {}).
28-
get('urlrewriter_class', UrlRewriter))
28+
config = kwargs.get('config', {})
29+
self.urlrewriter_class = config.get('urlrewriter_class', UrlRewriter)
30+
31+
self.enable_coll_info = config.get('enable_coll_info', False)
2932

3033
def __call__(self, env):
3134
request_uri = self.ensure_rel_uri_set(env)
@@ -43,6 +46,13 @@ def __call__(self, env):
4346
if request_uri in ['/', '/index.html', '/index.htm']:
4447
return self.render_home_page(env)
4548

49+
if self.enable_coll_info and request_uri in ['/collinfo.json']:
50+
params = env.get('pywb.template_params', {})
51+
host = WbRequest.make_host_prefix(env)
52+
return self.info_view.render_response(env=env, host=host, routes=self.routes,
53+
content_type='application/json',
54+
**params)
55+
4656
return self.fallback(env, self) if self.fallback else None
4757

4858
def parse_request(self, route, env, matcher, coll, request_uri,

pywb/templates/collinfo.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[
2+
{% for route in routes %}
3+
{% if route | is_wb_handler %}
4+
{{ ',' if notfirst else '' }}
5+
{
6+
"id": "{{ route.path }}",
7+
"name": "{{ route.user_metadata.title if route.user_metadata.title else route.path }}",
8+
"timegate": "{{ host }}/{{route.path}}/",
9+
"timemap": "{{ host }}/{{route.path}}/timemap/*/"
10+
11+
}
12+
{% set notfirst = true %}
13+
{% endif %}
14+
{% endfor %}
15+
]

pywb/webapp/pywb_init.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,5 +381,6 @@ def create_wb_router(passed_config=None):
381381
abs_path=config.get('absolute_paths', True),
382382
home_view=init_view(config, 'home_html'),
383383
error_view=init_view(config, 'error_html'),
384+
info_view=init_view(config, 'info_json'),
384385
config=config
385386
)

tests/test_config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ proxy_options:
121121
use_client_rewrite: true
122122
use_wombat: true
123123

124+
125+
#enable coll info JSON
126+
enable_coll_info: true
127+
124128
# enable cdx server api for querying cdx directly (experimental)
125129
#enable_cdx_api: True
126130
# or specify suffix

tests/test_integration.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,13 @@ def test_error(self):
477477
assert resp.status_int == 400
478478
assert 'Invalid Url: http://?abc' in resp.body
479479

480-
#def test_invalid_config(self):
480+
481+
def test_coll_info_json(self):
482+
resp = self.testapp.get('/collinfo.json')
483+
assert resp.content_type == 'application/json'
484+
assert len(resp.json) == 9
485+
486+
#def test_invalid_config(self):
481487
# with raises(IOError):
482488
# init_app(create_wb_router,
483489
# load_yaml=True,

0 commit comments

Comments
 (0)