-
I'm following the example in the actix.rs website where web::scope("/")
.guard(guard::Host("www.rust-lang.org"))
.route("", web::to(|| async { HttpResponse::Ok().body("www") }))
.route("hello", web::to(|| async { HttpResponse::Ok().body("www") })) // <--- adding this routes to //hello
// .route("/hello", web::to(|| async { HttpResponse::Ok().body("www") })) // <--- alternatively, adding this *also* routes to //hello This means that in the above example, I tried a couple of solutions, but i can't think of a great way to create a collection of routes with a shared scope + guard. Couple of options:
.service(
web::scope("/").guard(Host("host1.com")).route(...)
)
.service(
web::scope("/").guard(Host("myhost.com")).route(...)
)
.service(
web::scope("/hello").guard(Host("host1.com")).route(...)
)
.service(
web::scope("").guard(Host("host1.com")).route("/", ...)
)
.service(
web::scope("").guard(Host("myhost.com")).route("/", ...).route("/hello", ...)
)
None of these options are optimal, so I'm not sure if I'm just missing something obvious. Is there a reason why Resources prepends leading slashes and is it possible to override that behavior? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The semantics of resource definitions is defined in detail over here: https://docs.rs/actix-web/4.5.1/actix_web/dev/struct.ResourceDef.html#pattern-format-and-matching-behavior In particular:
Therefore this behavior is intentional. The canonical way to avoid double slashes here is to use
|
Beta Was this translation helpful? Give feedback.
The semantics of resource definitions is defined in detail over here: https://docs.rs/actix-web/4.5.1/actix_web/dev/struct.ResourceDef.html#pattern-format-and-matching-behavior
In particular:
Therefore this behavior is intentional. The canonical way to avoid double slashes here is to use
web::scope("")
. This is also documented: https://docs.rs/actix-web/4.5.1/actix_web/struct.Scope.html#avoid-trailing-slashes.