actix / actix/actix-web

Resolving URIs to handlers

Abierto
#2,571 1 comentario 0 reacciones 1 asignado Reclamado por @TimoCak Ver en GitHub
A-web C-feature
Lenguaje dominante
Rust
Estrellas
24.8k
Forks
1.9k
Merge medio
23 h 10 min
PR fusionados (30 d)
26

Descripción

e### Motivation
Make it easier to support more complex and interconnected REST APIs by matching paths to the resource tree from inside a handler.

### Proposal

Effectively we need a method like:
```rust
impl HttpRequest {
fn match_path(&self, path: &str) -> Option>;
}
```
that matches `path` against the resource map tree and returns a Path with filled in dynamic segments.

### Discussed in https://github.com/actix/actix-web/discussions/2559

Originally posted by **sazzer** December 31, 2021
Hi there,

The existence of `HttpRequest::url_for` is fantastic for building real REST APIs. I can use this to generate URIs for other resources so that I can correctly generate the hypermedia graph.

However, what I can't quite work out how to do is the opposite. To take a URL that was was provided to me and convert it back into the details of what it resolves to.

If I understand it right, I can use `HttpRequest::resource_map().match_name()` to get the *name* of the handler that matches a given URL, but this doesn't give me the binds.

For example, if I had:
```rust
config.service(
web::resource("/users/{id}")
.name("users_user_by_id")
.route(web::get().to(get::handler)),
);
```

Then I could use `req.resource_map().match_name("http://localhost:8000/users/abc")` to get `Some("users_user_by_id")`, but that would *not* tell me that the `{id}` bind was assigned the value `"abc"`.

Is there any way to get this full expansion?

Cheers

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.