mirror of
https://github.com/tokio-rs/axum.git
synced 2025-03-20 22:18:51 +01:00
Add MethodRouter::{into_make_service, into_make_service_with_connect_info}
(#1010)
This commit is contained in:
parent
b8514cf1c2
commit
a3a32f493e
2 changed files with 73 additions and 3 deletions
|
@ -12,8 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
the request body will not be checked. If they do have a `Content-Length` header they'll be
|
the request body will not be checked. If they do have a `Content-Length` header they'll be
|
||||||
rejected. This allows `ContentLengthLimit` to be used as middleware around several routes,
|
rejected. This allows `ContentLengthLimit` to be used as middleware around several routes,
|
||||||
including `GET` routes ([#989])
|
including `GET` routes ([#989])
|
||||||
|
- **added:** Add `MethodRouter::{into_make_service, into_make_service_with_connect_info}` ([#1010])
|
||||||
|
|
||||||
[#989]: https://github.com/tokio-rs/axum/pull/989
|
[#989]: https://github.com/tokio-rs/axum/pull/989
|
||||||
|
[#1010]: https://github.com/tokio-rs/axum/pull/1010
|
||||||
|
|
||||||
# 0.5.4 (26. April, 2022)
|
# 0.5.4 (26. April, 2022)
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
|
use super::IntoMakeService;
|
||||||
use crate::{
|
use crate::{
|
||||||
body::{boxed, Body, Bytes, Empty, HttpBody},
|
body::{boxed, Body, Bytes, Empty, HttpBody},
|
||||||
error_handling::{HandleError, HandleErrorLayer},
|
error_handling::{HandleError, HandleErrorLayer},
|
||||||
|
extract::connect_info::IntoMakeServiceWithConnectInfo,
|
||||||
handler::Handler,
|
handler::Handler,
|
||||||
http::{Method, Request, StatusCode},
|
http::{Method, Request, StatusCode},
|
||||||
response::Response,
|
response::Response,
|
||||||
routing::{Fallback, MethodFilter, Route},
|
routing::{future::RouteFuture, Fallback, MethodFilter, Route},
|
||||||
BoxError,
|
BoxError,
|
||||||
};
|
};
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
|
@ -586,6 +588,74 @@ where
|
||||||
chained_handler_fn!(post, POST);
|
chained_handler_fn!(post, POST);
|
||||||
chained_handler_fn!(put, PUT);
|
chained_handler_fn!(put, PUT);
|
||||||
chained_handler_fn!(trace, TRACE);
|
chained_handler_fn!(trace, TRACE);
|
||||||
|
|
||||||
|
/// Convert the handler into a [`MakeService`].
|
||||||
|
///
|
||||||
|
/// This allows you to serve a single handler if you don't need any routing:
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use axum::{
|
||||||
|
/// Server,
|
||||||
|
/// handler::Handler,
|
||||||
|
/// http::{Uri, Method},
|
||||||
|
/// response::IntoResponse,
|
||||||
|
/// routing::get,
|
||||||
|
/// };
|
||||||
|
/// use std::net::SocketAddr;
|
||||||
|
///
|
||||||
|
/// async fn handler(method: Method, uri: Uri, body: String) -> impl IntoResponse {
|
||||||
|
/// format!("received `{} {}` with body `{:?}`", method, uri, body)
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// let router = get(handler).post(handler);
|
||||||
|
///
|
||||||
|
/// # async {
|
||||||
|
/// Server::bind(&SocketAddr::from(([127, 0, 0, 1], 3000)))
|
||||||
|
/// .serve(router.into_make_service())
|
||||||
|
/// .await?;
|
||||||
|
/// # Ok::<_, hyper::Error>(())
|
||||||
|
/// # };
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// [`MakeService`]: tower::make::MakeService
|
||||||
|
pub fn into_make_service(self) -> IntoMakeService<Self> {
|
||||||
|
IntoMakeService::new(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convert the router into a [`MakeService`] which stores information
|
||||||
|
/// about the incoming connection.
|
||||||
|
///
|
||||||
|
/// See [`Router::into_make_service_with_connect_info`] for more details.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use axum::{
|
||||||
|
/// Server,
|
||||||
|
/// handler::Handler,
|
||||||
|
/// response::IntoResponse,
|
||||||
|
/// extract::ConnectInfo,
|
||||||
|
/// routing::get,
|
||||||
|
/// };
|
||||||
|
/// use std::net::SocketAddr;
|
||||||
|
///
|
||||||
|
/// async fn handler(ConnectInfo(addr): ConnectInfo<SocketAddr>) -> impl IntoResponse {
|
||||||
|
/// format!("Hello {}", addr)
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// let router = get(handler).post(handler);
|
||||||
|
///
|
||||||
|
/// # async {
|
||||||
|
/// Server::bind(&SocketAddr::from(([127, 0, 0, 1], 3000)))
|
||||||
|
/// .serve(router.into_make_service_with_connect_info::<SocketAddr>())
|
||||||
|
/// .await?;
|
||||||
|
/// # Ok::<_, hyper::Error>(())
|
||||||
|
/// # };
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// [`MakeService`]: tower::make::MakeService
|
||||||
|
/// [`Router::into_make_service_with_connect_info`]: crate::routing::Router::into_make_service_with_connect_info
|
||||||
|
pub fn into_make_service_with_connect_info<C>(self) -> IntoMakeServiceWithConnectInfo<Self, C> {
|
||||||
|
IntoMakeServiceWithConnectInfo::new(self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<ReqBody, E> MethodRouter<ReqBody, E> {
|
impl<ReqBody, E> MethodRouter<ReqBody, E> {
|
||||||
|
@ -958,8 +1028,6 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
use crate::routing::future::RouteFuture;
|
|
||||||
|
|
||||||
impl<B, E> Service<Request<B>> for MethodRouter<B, E>
|
impl<B, E> Service<Request<B>> for MethodRouter<B, E>
|
||||||
where
|
where
|
||||||
B: HttpBody,
|
B: HttpBody,
|
||||||
|
|
Loading…
Add table
Reference in a new issue