docs: fix route_service example for Body API (#3890)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by chenchen · committed by GitHub · parent af1345b · Browse files · 2 files·+24 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+24 added−16 removed
- other:+24 added−16 removed
Provenance
- Commit
0b9325b- Tree
b8b5d2c- Author
chenchen <Kayano04@outlook.jp>- Committer
GitHub <noreply@github.com>- Parent
af1345b
2 files changed+24 added−16 removed
modified:axum/src/docs/routing/route_service.md+21 added-16 removed
12345678910111213514151617918191112201421162223242526272128232425293031273233293435363733343839404142433944454642474849505124 unmodified linesAdd another route to the router that calls a [`Service`].
Unlike [`Router::route`], this accepts a [`Service`] directly rather than a[`MethodRouter`]. All HTTP methods are forwarded to the service.
The service receives a [`Request`] (whose body type is [`Body`]) and mustreturn a response that implements [`IntoResponse`]. The error type must be[`Infallible`].
See [`any_service`] and [`get_service`] for using services with [`Router::route`].
# Example
```rust,no_run```rustuse axum::{ Router, body::Body, routing::{any_service, get_service}, routing::any_service, extract::Request, http::StatusCode, error_handling::HandleErrorLayer,};use tower_http::services::ServeFile;use http::Response;use std::{convert::Infallible, io};use std::convert::Infallible;use tower::service_fn;use tower_http::services::ServeFile;
let app = Router::new() .route( // Any request to `/` goes to a service "/", // 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 // `route` takes a `MethodRouter`. `any_service` matches all methods; // use `get_service` / `post_service` / ... to match specific methods. any_service(service_fn(|_: Request| async { let res = Response::new(Body::from("Hi from `GET /`")); let res = Response::new(Body::from("Hi from `/`")); Ok::<_, Infallible>(res) })) })), ) .route_service( "/foo", // This service's response body is `axum::body::BoxBody` so // it can be routed to directly. // `route_service` takes any `Service<Request>` whose response // implements `IntoResponse`. service_fn(|req: Request| async move { let body = Body::from(format!("Hi from `{} /foo`", req.method())); let res = Response::new(body); Ok::<_, Infallible>(res) }) }), ) .route_service( // GET `/static/Cargo.toml` goes to a service from tower-http // Services from tower-http work the same way. "/static/Cargo.toml", ServeFile::new("Cargo.toml"), );# let _: Router = app;24 unmodified linesmodified:axum/CHANGELOG.md+3 added
30 unmodified lines31 - **fixed:** Responses to `HEAD` will not accidentally reply with `content-length: 0` anymore ([#3742])32 - **added:** Add `MethodFilter::QUERY`, `routing::query[_service]` and `MethodRouter::query[_service]` ([#3801])33 - **fixed:** `MethodRouter::merge` no longer lists a method twice in the `Allow`34 header after merging `get` and `head` ([#3836])35+- **changed:** Update `Router::route_service` docs for `axum::body::Body` instead of36+ the removed `BoxBody` type ([#3890])37 38 [#3158]: https://github.com/tokio-rs/axum/pull/315839 [#3261]: https://github.com/tokio-rs/axum/pull/326140 [#3205]: https://github.com/tokio-rs/axum/pull/32059 unmodified lines50 [#3757]: https://github.com/tokio-rs/axum/pull/375751 [#3829]: https://github.com/tokio-rs/axum/pull/382952 [#3801]: https://github.com/tokio-rs/axum/pull/380153 [#3867]: https://github.com/tokio-rs/axum/pull/386754+[#3890]: https://github.com/tokio-rs/axum/pull/389055 56 # 0.8.957 58 - **added:** `WebSocketUpgrade::{requested_protocols, set_selected_protocol}` for more2409 unmodified lines