klein
klein copied to clipboard
allow for "overlay" routing
Sometimes I have a structure like this:
@route("/a/", branch=True)
def some_tree(request):
return static.File("...")
@route("/a/foo/bar/special_thing.txt")
def some_overlaid_resource(request):
return EventSource(...)
I'd really like that second route to show up if that specific route is requested, but the first to take over for everything else within that hierarchy.
Probably this is a bug in Werkzeug, but I am experiencing it with Klein so I'm starting here.
Okay wait a second maybe this already works if you put @route("/a/foo/bar/special_thing.txt")
first? I can't tell if I'm depending on some accident of hash ordering or something like that..
I'd strongly suspect that this is Werkzeug being funky. From https://github.com/mitsuhiko/werkzeug/blob/2b2d921eea7d1f896ce436ec5238890abe8b81bf/werkzeug/routing.py#L855 :
Current implementation:
- rules without any arguments come first for performance reasons only as we expect them to match faster and some common ones usually don't have any arguments (index pages etc.)
- The more complex rules come first so the second argument is the negative length of the number of weights.
- lastly we order by the actual weights.