How to match route with multiple slashes in rouille router?

I want to build a simple file server returning a html file with a directory tree, but I can't figure out how I can make a route that takes a path with "/" in rouille, using the router! macro:

router!(request,
    (GET) (/{path: PathBuf}) => {
        handle_path(path.as_path())
    },
    _ => Response::empty_400()
)

This route does not seem to match something like localhost:8000/src/main.rs. However I would like to have arbitiarily deep directories in my server root, so I would like to extract a full path from a route url. How would I do this with rouille?

cc @tomaka

I don't see a direct provision for this in the old-style macro of router! [1]. router! focuses on matching an exact path.

That said, I would use the default route _ => {} to analyze the path.

router!(request,
    _ => {
        let req_path = {
            let uri = request.url();
            let pos = uri.find('?').unwrap_or(uri.len());
            &uri[..pos]
        };

        if request.method() == "GET" {
            handle_path(req_path.as_path())
        } else {
            Response::empty_404()
        }
    }
}

I would go further and use the router! source [1] for inspiration to build a separate macro (e.g. route_path!) specifically for the (METHOD) [path] => {} syntax, and call that from router!'s default route to improve the readability of the routing DSL.

[1]: https://tomaka.github.io/rouille/src/rouille/router.rs.html#66-169

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.