trollfot / trinket

A curio HTTP server
7 stars 2 forks source link

Routing is more work than it should be #7

Open rt121212121 opened 4 years ago

rt121212121 commented 4 years ago

There's something wrong with routing, where if I register routes under a parent path that are shallower before deeper ones, the deeper ones won't match.

This is what I need to do to get my wwwroot directory serving files:

    web_paths = []
    for root_path, dirnames, filenames in os.walk(app.wwwroot_path):
        if len(filenames):
            web_path = os.path.relpath(root_path, app.wwwroot_path).replace(os.path.sep, "/")
            web_paths.append(web_path)

    # Deeper paths need to be routed first so as to not override shallower paths.
    for web_path in sorted(web_paths, key=len, reverse=True):
        if web_path == ".":
            server.route("/{filename}")(partial(serve_file, app))
        else:
            server.route("/"+ web_path +"/{filename}")(partial(serve_file, app))

Note the reverse to ensure that deeper paths are registered first. Now the routing dependency claims that {filename} does not match /, but surely it must be doing so.