How you'd add such a route to an app depends on what library or libraries that app is using to implement WSGI. I see that the app.py
file you linked to is using werkzeug
(as well as static
).
Here are some useful references for routing with placeholders in werkzeug
:
I've barely used werkzeug
and won't claim this is definitely the best approach, but one option would be to add another WSGI app via werkzeug
to the wsgi.DispatcherMiddleware
call at the bottom of that file.
Here's some thrown-together sample code to get you started in the context of the app.py
file you shared. Try deleting everything after this line and replacing it with this code:
from werkzeug.routing import Map, Rule
from werkzeug.exceptions import HTTPException
my_url_map = Map([
# Note that this rule builds on the `/cat_articles` prefix used in the `DispatcherMiddleware` call further down
Rule('/<article_id>', endpoint='get_cat_article')
])
def cat_app(environ, start_response):
urls = my_url_map.bind_to_environ(environ)
try:
endpoint, args = urls.match()
except HTTPException, e:
return e(environ, start_response)
if endpoint == 'get_cat_article':
# Note that werkzeug also provides objects for building responses: http://werkzeug.pocoo.org/docs/0.14/wrappers
start_response('200 OK', [('Content-Type', 'text/plain')])
return ['Finally, a cat-centric article about {0}'.format(args['article_id'])]
else:
start_response('404 Not Found', [('Content-Type', 'text/plain')])
return ['Nothing is here...']
application = RequestHeaderSanitiser(app)
application = ResponseHeaderSanitiser(application)
application = Blocker(application)
application = UserAgentDecorator(application, 'Hypothesis-Via')
application = wsgi.DispatcherMiddleware(application, {
'/cat_articles': cat_app,
'/favicon.ico': static.Cling('static/favicon.ico'),
'/robots.txt': static.Cling('static/robots.txt'),
'/static': static.Cling('static/'),
'/static/__pywb': static.Cling(resource_filename('pywb', 'static/')),
'/static/__shared/viewer/web/viewer.html': redirect_old_viewer,
'/h': redirect_strip_matched_path,
})
With that code, the path /cat_articles/plants
should return:
Finally, a cat-centric article about plants