mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-20 00:00:11 +02:00
Bring back Handler::into_service (#212)
It was removed as part of https://github.com/tokio-rs/axum/pull/184 but I do actually think it has some utility. So makes sense to keep even if axum doesn't use it directly for routing.
This commit is contained in:
@@ -21,6 +21,9 @@ use std::{
|
||||
use tower::{BoxError, Layer, Service, ServiceExt};
|
||||
|
||||
pub mod future;
|
||||
mod into_service;
|
||||
|
||||
pub use self::into_service::IntoService;
|
||||
|
||||
/// Route requests to the given handler regardless of the HTTP method of the
|
||||
/// request.
|
||||
@@ -255,6 +258,34 @@ pub trait Handler<B, T>: Clone + Send + Sized + 'static {
|
||||
{
|
||||
Layered::new(layer.layer(any(self)))
|
||||
}
|
||||
|
||||
/// Convert the handler into a [`Service`].
|
||||
///
|
||||
/// 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,
|
||||
/// };
|
||||
/// use tower::make::Shared;
|
||||
/// use std::net::SocketAddr;
|
||||
///
|
||||
/// async fn handler(method: Method, uri: Uri, body: String) -> impl IntoResponse {
|
||||
/// format!("received `{} {}` with body `{:?}`", method, uri, body)
|
||||
/// }
|
||||
///
|
||||
/// let service = handler.into_service();
|
||||
///
|
||||
/// # async {
|
||||
/// Server::bind(&SocketAddr::from(([127, 0, 0, 1], 3000)))
|
||||
/// .serve(Shared::new(service))
|
||||
/// .await?;
|
||||
/// # Ok::<_, hyper::Error>(())
|
||||
/// # };
|
||||
/// ```
|
||||
fn into_service(self) -> IntoService<Self, B, T> {
|
||||
IntoService::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
|
||||
Reference in New Issue
Block a user