Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router::route_service() example code is out of date #2536

Open
jwodder opened this issue Jan 25, 2024 · 7 comments
Open

Router::route_service() example code is out of date #2536

jwodder opened this issue Jan 25, 2024 · 7 comments
Labels
T-docs Topic: documentation

Comments

@jwodder
Copy link

jwodder commented Jan 25, 2024

The current documentation for axum::Router::route_service() has an example that contains the following lines:

        // Services whose response body is not `axum::body::BoxBody`
        // can be wrapped in `axum::routing::any_service` (or one of the other routing filters)
        // to have the response body mapped
        any_service(service_fn(|_: Request| async {
            let res = Response::new(Body::from("Hi from `GET /`"));
            Ok::<_, Infallible>(res)
        }))

        ...

        // This service's response body is `axum::body::BoxBody` so
        // it can be routed to directly.
        service_fn(|req: Request| async move {
            let body = Body::from(format!("Hi from `{} /foo`", req.method()));
            let res = Response::new(body);
            Ok::<_, Infallible>(res)
        })

However, despite what the comments in the code say, both responses appear to have axum::body::Body as their bodies, and there is currently no axum::body::BoxBody in the crate. I assume that BoxBody was removed in an earlier version and the example was only partially updated, but that just raises the question: What is any_service() good for now?

@davidpdrsn
Copy link
Member

Ah yes that example is indeed out of date. axum always uses axum::body::Body for requests and responses since 0.7.

What is any_service() good for now?

It can still be used to route to any service regardless of the HTTP method.

@davidpdrsn davidpdrsn changed the title Router::route_service() example code out of date? Router::route_service() example code if out of date Jan 25, 2024
@davidpdrsn davidpdrsn changed the title Router::route_service() example code if out of date Router::route_service() example code is out of date Jan 25, 2024
@davidpdrsn davidpdrsn added the T-docs Topic: documentation label Jan 25, 2024
@jwodder
Copy link
Author

jwodder commented Jan 25, 2024

What is any_service() good for now?

It can still be used to route to any service regardless of the HTTP method.

So what happens if I supply a service_fn() but don't wrap it in any_service()? Will only GET requests be routed to it?

@davidpdrsn
Copy link
Member

"Supply" it how?

@jwodder
Copy link
Author

jwodder commented Jan 25, 2024

Ah, I missed that the non-any_service() bit of the example was using route_service() instead of route(). So the main point of any_service() is to let you pass a service to route() instead of to route_service()?

@davidpdrsn
Copy link
Member

Exactly. You might also wanna run the MethodRouter, without using Router at all.

@JosiahParry
Copy link

It would be great if the example code or documentation could illustrate how to use this with State and Path.

I have a function like so

async fn handler(State(db): State<Arc<Database>>,
    Path(id): Path<String>,
) 

and providing to route_service("/:id", service_fn(handler)) results in a compiler error.

There isn't any documentation or examples to figure out how to resolve this unfortunately

@jplatte
Copy link
Member

jplatte commented Aug 8, 2024

@JosiahParry service_fn does not support axum handler functions, it is much more restrictive. Please can you open a separate question in the Q&A section?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-docs Topic: documentation
Projects
None yet
Development

No branches or pull requests

4 participants