1

我需要warp为所有以扩展名结尾的 URL 创建一个路由,无论 URL 路径中的段数如何,例如path.extsome/path.ext并且some/other/path.ext应该由相同的路由处理。

我越接近解决方案就是这种技术,但它要求路径段在编译时是固定的并且是已知的,这不是我的情况。

4

1 回答 1

0

I decided to settle with a simple workaround. Posting here so maybe it can help someone.

In order to capture all URL paths ending with .ext in one route, while capturing all other paths not ending with .ext in another route, I wrote this:

let extension_path = warp::get().and_then(handle_extension);
let regular_path = warp::get().and_then(handle_regular);

let routes = extension_path.or(regular_path);

Then, in the handle_extension handler I issue a warp::Rejection if the path does not contains the .ext suffix, which will send the request to be processed by the next handler handle_regular.

This is a core paradigm in warp, you use rejections to perform control flow inside the routing filter chains.

I have opened an issue describing this limitation.

于 2021-02-27T15:25:04.217 回答