1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-13 02:39:32 +00:00
actix-web/src/handler.rs

91 lines
2.8 KiB
Rust
Raw Normal View History

2019-11-20 17:33:22 +00:00
use std::future::Future;
2017-10-07 04:48:14 +00:00
2021-12-04 19:40:47 +00:00
use actix_service::{boxed, fn_service};
2018-05-02 00:30:06 +00:00
use crate::{
2021-12-04 19:40:47 +00:00
body::MessageBody,
service::{BoxedHttpServiceFactory, ServiceRequest, ServiceResponse},
BoxError, FromRequest, HttpResponse, Responder,
};
2017-11-03 20:35:34 +00:00
/// A request handler is an async function that accepts zero or more parameters that can be
2021-12-04 19:40:47 +00:00
/// extracted from a request (i.e., [`impl FromRequest`]) and returns a type that can be converted
/// into an [`HttpResponse`] (that is, it impls the [`Responder`] trait).
2020-12-26 21:46:19 +00:00
///
/// If you got the error `the trait Handler<_, _, _> is not implemented`, then your function is not
2021-12-04 19:40:47 +00:00
/// a valid handler. See <https://actix.rs/docs/handlers> for more information.
///
/// [`impl FromRequest`]: crate::FromRequest
2020-12-26 21:46:19 +00:00
pub trait Handler<T, R>: Clone + 'static
2019-03-02 06:51:32 +00:00
where
2020-12-26 21:46:19 +00:00
R: Future,
R::Output: Responder,
2019-03-02 06:51:32 +00:00
{
fn call(&self, param: T) -> R;
}
2018-05-02 00:19:15 +00:00
2021-12-04 19:40:47 +00:00
pub(crate) fn handler_service<F, T, R>(handler: F) -> BoxedHttpServiceFactory
2020-12-23 15:47:07 +00:00
where
2020-12-26 21:46:19 +00:00
F: Handler<T, R>,
2020-12-23 15:47:07 +00:00
T: FromRequest,
2020-12-26 21:46:19 +00:00
R: Future,
R::Output: Responder,
2021-12-04 19:40:47 +00:00
<R::Output as Responder>::Body: MessageBody,
<<R::Output as Responder>::Body as MessageBody>::Error: Into<BoxError>,
2020-12-23 15:47:07 +00:00
{
2021-11-17 20:11:35 +00:00
boxed::factory(fn_service(move |req: ServiceRequest| {
let handler = handler.clone();
2021-12-04 19:40:47 +00:00
2021-11-17 20:11:35 +00:00
async move {
let (req, mut payload) = req.into_parts();
2021-12-04 19:40:47 +00:00
2021-11-17 20:11:35 +00:00
let res = match T::from_request(&req, &mut payload).await {
Err(err) => HttpResponse::from_error(err),
2021-12-04 19:40:47 +00:00
Ok(data) => handler
.call(data)
.await
.respond_to(&req)
.map_into_boxed_body(),
2021-11-17 20:11:35 +00:00
};
2021-12-04 19:40:47 +00:00
2021-11-17 20:11:35 +00:00
Ok(ServiceResponse::new(req, res))
2019-11-20 17:33:22 +00:00
}
2021-11-17 20:11:35 +00:00
}))
2018-03-29 22:41:13 +00:00
}
2021-12-04 19:40:47 +00:00
/// Generates a [`Handler`] trait impl for N-ary functions where N is specified with a sequence of
/// space separated type parameters.
///
/// # Examples
/// ```ignore
/// factory_tuple! {} // implements Handler for types: fn() -> Res
/// factory_tuple! { A B C } // implements Handler for types: fn(A, B, C) -> Res
/// ```
2021-03-19 16:30:53 +00:00
macro_rules! factory_tuple ({ $($param:ident)* } => {
impl<Func, $($param,)* Res> Handler<($($param,)*), Res> for Func
where Func: Fn($($param),*) -> Res + Clone + 'static,
2020-12-26 21:46:19 +00:00
Res: Future,
Res::Output: Responder,
2019-03-02 06:51:32 +00:00
{
2021-03-19 16:30:53 +00:00
#[allow(non_snake_case)]
fn call(&self, ($($param,)*): ($($param,)*)) -> Res {
(self)($($param,)*)
2019-03-02 06:51:32 +00:00
}
2018-03-29 22:41:13 +00:00
}
2019-03-02 06:51:32 +00:00
});
2021-03-19 16:30:53 +00:00
factory_tuple! {}
factory_tuple! { A }
factory_tuple! { A B }
factory_tuple! { A B C }
factory_tuple! { A B C D }
factory_tuple! { A B C D E }
factory_tuple! { A B C D E F }
factory_tuple! { A B C D E F G }
factory_tuple! { A B C D E F G H }
factory_tuple! { A B C D E F G H I }
factory_tuple! { A B C D E F G H I J }
factory_tuple! { A B C D E F G H I J K }
factory_tuple! { A B C D E F G H I J K L }