From dfcb25ae81a129ce5ca17c19ac01f5288593b724 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Nov 10 2015 10:59:48 +0000 Subject: [PATCH 1/4] Add the possibility to prefix the URLs via the configuration file --- diff --git a/mdapi/__init__.py b/mdapi/__init__.py index 943f9b8..5468f83 100644 --- a/mdapi/__init__.py +++ b/mdapi/__init__.py @@ -180,12 +180,26 @@ def index(request): @asyncio.coroutine def init(loop): app = web.Application(loop=loop) - app.router.add_route('GET', '/', index) - app.router.add_route('GET', '/branches', list_branches) - app.router.add_route('GET', '/{branch}/pkg/{name}', get_pkg) - app.router.add_route('GET', '/{branch}/files/{name}', get_pkg_files) app.router.add_route( - 'GET', '/{branch}/changelog/{name}', get_pkg_changelog) + 'GET', + '%s/' % CONFIG.get('PREFIX', ''), + index) + app.router.add_route( + 'GET', + '%s/branches' % CONFIG.get('PREFIX', ''), + list_branches) + app.router.add_route( + 'GET', + '%s/{branch}/pkg/{name}' % CONFIG.get('PREFIX', ''), + get_pkg) + app.router.add_route( + 'GET', + '%s/{branch}/files/{name}' % CONFIG.get('PREFIX', ''), + get_pkg_files) + app.router.add_route( + 'GET', + '%s/{branch}/changelog/{name}' % CONFIG.get('PREFIX', ''), + get_pkg_changelog) srv = yield from loop.create_server( app.make_handler(), From fffd4aa501bd754375c2967ea3c9a32342df2ae1 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Nov 10 2015 11:20:39 +0000 Subject: [PATCH 2/4] If there is a prefix, show the front page at both /prefix and /prefix/ --- diff --git a/mdapi/__init__.py b/mdapi/__init__.py index 5468f83..783b98f 100644 --- a/mdapi/__init__.py +++ b/mdapi/__init__.py @@ -180,6 +180,11 @@ def index(request): @asyncio.coroutine def init(loop): app = web.Application(loop=loop) + if CONFIG.get('PREFIX'): + app.router.add_route( + 'GET', + '%s' % CONFIG.get('PREFIX', ''), + index) app.router.add_route( 'GET', '%s/' % CONFIG.get('PREFIX', ''), From 0dd8b85b8e15c6649ba5167ac707437718c1f1a3 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Nov 10 2015 12:04:37 +0000 Subject: [PATCH 3/4] Simplify the routing by using a list of tuples --- diff --git a/mdapi/__init__.py b/mdapi/__init__.py index 783b98f..57269bb 100644 --- a/mdapi/__init__.py +++ b/mdapi/__init__.py @@ -180,31 +180,20 @@ def index(request): @asyncio.coroutine def init(loop): app = web.Application(loop=loop) - if CONFIG.get('PREFIX'): - app.router.add_route( - 'GET', - '%s' % CONFIG.get('PREFIX', ''), - index) - app.router.add_route( - 'GET', - '%s/' % CONFIG.get('PREFIX', ''), - index) - app.router.add_route( - 'GET', - '%s/branches' % CONFIG.get('PREFIX', ''), - list_branches) - app.router.add_route( - 'GET', - '%s/{branch}/pkg/{name}' % CONFIG.get('PREFIX', ''), - get_pkg) - app.router.add_route( - 'GET', - '%s/{branch}/files/{name}' % CONFIG.get('PREFIX', ''), - get_pkg_files) - app.router.add_route( - 'GET', - '%s/{branch}/changelog/{name}' % CONFIG.get('PREFIX', ''), - get_pkg_changelog) + routes = [] + prefix = CONFIG.get('PREFIX', '') + if prefix: + routes.append(('%s', index)) + + routes.extend([ + ('%s/', index), + ('%s/branches', list_branches), + ('%s/{branch}/pkg/{name}', get_pkg), + ('%s/{branch}/files/{name}', get_pkg_files), + ('%s/{branch}/changelog/{name}', get_pkg_changelog), + ]) + for route in routes: + app.router.add_route('GET', route[0] % prefix, route[1]) srv = yield from loop.create_server( app.make_handler(), From 5a1febb37bcb34cf2d95d0ee888254be5f8dae77 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Nov 10 2015 14:30:37 +0000 Subject: [PATCH 4/4] Drop the '%s' in the routes by just prefixing the prefix --- diff --git a/mdapi/__init__.py b/mdapi/__init__.py index 57269bb..199e64f 100644 --- a/mdapi/__init__.py +++ b/mdapi/__init__.py @@ -186,14 +186,14 @@ def init(loop): routes.append(('%s', index)) routes.extend([ - ('%s/', index), - ('%s/branches', list_branches), - ('%s/{branch}/pkg/{name}', get_pkg), - ('%s/{branch}/files/{name}', get_pkg_files), - ('%s/{branch}/changelog/{name}', get_pkg_changelog), + ('/', index), + ('/branches', list_branches), + ('/{branch}/pkg/{name}', get_pkg), + ('/{branch}/files/{name}', get_pkg_files), + ('/{branch}/changelog/{name}', get_pkg_changelog), ]) for route in routes: - app.router.add_route('GET', route[0] % prefix, route[1]) + app.router.add_route('GET', prefix + route[0], route[1]) srv = yield from loop.create_server( app.make_handler(),