mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-23 00:00:15 +02:00
Clarify required response body type when routing to tower::Services (#69)
This commit is contained in:
+4
-3
@@ -9,11 +9,12 @@ pub use hyper::body::Body;
|
||||
|
||||
/// A boxed [`Body`] trait object.
|
||||
///
|
||||
/// This is used in axum as the response body type for applications. Its necessary to unify
|
||||
/// multiple response bodies types into one.
|
||||
/// This is used in axum as the response body type for applications. Its
|
||||
/// necessary to unify multiple response bodies types into one.
|
||||
pub type BoxBody = http_body::combinators::BoxBody<Bytes, BoxStdError>;
|
||||
|
||||
pub(crate) fn box_body<B>(body: B) -> BoxBody
|
||||
/// Convert a [`http_body::Body`] into a [`BoxBody`].
|
||||
pub fn box_body<B>(body: B) -> BoxBody
|
||||
where
|
||||
B: http_body::Body<Data = Bytes> + Send + Sync + 'static,
|
||||
B::Error: Into<BoxError>,
|
||||
|
||||
+13
@@ -487,11 +487,24 @@
|
||||
//! let app = route(
|
||||
//! // Any request to `/` goes to a service
|
||||
//! "/",
|
||||
//! // Services who's response body is not `axum::body::BoxBody`
|
||||
//! // can be wrapped in `axum::service::any` (or one of the other routing filters)
|
||||
//! // to have the response body mapped
|
||||
//! service::any(service_fn(|_: Request<Body>| async {
|
||||
//! let res = Response::new(Body::from("Hi from `GET /`"));
|
||||
//! Ok(res)
|
||||
//! }))
|
||||
//! ).route(
|
||||
//! "/foo",
|
||||
//! // This service's response body is `axum::body::BoxBody` so
|
||||
//! // it can be routed to directly.
|
||||
//! service_fn(|req: Request<Body>| async move {
|
||||
//! let body = Body::from(format!("Hi from `{} /foo`", req.method()));
|
||||
//! let body = axum::body::box_body(body);
|
||||
//! let res = Response::new(body);
|
||||
//! Ok(res)
|
||||
//! })
|
||||
//! ).route(
|
||||
//! // GET `/static/Cargo.toml` goes to a service from tower-http
|
||||
//! "/static/Cargo.toml",
|
||||
//! service::get(ServeFile::new("Cargo.toml"))
|
||||
|
||||
Reference in New Issue
Block a user