salvo-rs / salvo

A powerful web framework built with a simplified design.
https://salvo.rs
Apache License 2.0
3.21k stars 187 forks source link

Can't get `**rest_path` #889

Closed markcda closed 1 week ago

markcda commented 1 week ago

Describe the bug Can't get **rest_path

To Reproduce Steps to reproduce the behavior:

  1. Create an example-path in examples folder.
  2. Insert this into Cargo.toml:
    
    [package]
    name = "example-path"
    version.workspace = true
    edition.workspace = true
    publish.workspace = true

[dependencies] salvo = { workspace = true } tokio = { workspace = true, features = ["macros"] } tracing.workspace = true tracing-subscriber.workspace = true

and this into `main.rs`:
```rust
use salvo::prelude::*;

#[handler]
async fn hello(req: &Request) -> &'static str {
    let rest_path = req.param::<String>("**rest_path").ok_or("Cannot get rest path.").unwrap();
    "Hello World"
}

#[tokio::main]
async fn main() {
    tracing_subscriber::fmt().init();

    // only allow access from http://localhost:5800/, http://0.0.0.0:5800/ will get not found page.
    let router = Router::with_path("assets/<**rest_path>")
        .get(hello);

    let acceptor = TcpListener::new("0.0.0.0:5800").bind().await;
    Server::new(acceptor).serve(router).await;
}
  1. Run: cargo run --bin example-path
  2. Create a request: curl localhost:5800/assets/wherever
  3. Get an error:
    thread 'tokio-runtime-worker' panicked at path/src/main.rs:5:101:
    called `Result::unwrap()` on an `Err` value: "Cannot get rest path."
    note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Expected behavior No error.

Desktop (please complete the following information):

chrislearn commented 1 week ago

Change this line to:

 let rest_path = req.param::<String>("rest_path").ok_or("Cannot get rest path.").unwrap();
chrislearn commented 1 week ago

In old version salvo use **rest_path to get the rest path, but **rest_path is not a valid variable name and would cause problems when parsing OpenAPI, so we made a change.

markcda commented 1 week ago

Ok, thanks! Then docs in the site are needed to change too (https://salvo.rs/book/concepts/router.html#path-filter)

chrislearn commented 1 week ago

Thanks for your reminder, the document has been fixed.