如何捕捉火箭的所有路线?

How to catch all routes in rocket?

我到处查,只找到this github问题,不过是5年前的事了,rocket变化很大。 有没有办法在 rocket.

我尝试使用 github 问题中的代码,但是火箭 api 已经更新了很多所以我不知道如何。

您想要做的是 Rocket 中“手动路由”的一部分,您发现的问题中 linked 的代码现在已失效 link,但是有一个 similar example 在当前版本中。
它使用 Handler trait with its impl for functions:

fn hi<'r>(req: &'r Request, _: Data<'r>) -> route::BoxFuture<'r> {
    route::Outcome::from(req, "Hello!").pin()
}

#[rocket::launch]
fn rocket() -> _ {
    let hello = Route::ranked(2, Get, "/", hi);

    rocket::build()
        .mount("/", vec![hello])
}

如果您需要多种方法,您可以按照问题中描述的相同方式遍历它们,并使用

创建路由
#[rocket::launch]
fn rocket() -> _ {
    use rocket::http::Method::*;
    let mut routes = vec![];
    for method in &[Get, Put, Post, Delete, Options, Head, Trace, Connect, Patch] {
        routes.push(Route::new(*method, "/git/<path..>", git_http));
    }
    rocket::build().mount("/", routes)
}

<path..> 捕获所有段(我的测试默认捕获查询参数),您可以使用 Request 的方法获取它们。