2019-11-20 23:33:22 +06:00
|
|
|
use std::future::Future;
|
2017-10-06 21:48:14 -07:00
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
use actix_service::{boxed, fn_service};
|
2018-05-01 17:30:06 -07:00
|
|
|
|
2021-04-09 18:07:10 +01:00
|
|
|
use crate::{
|
2021-12-04 19:40:47 +00:00
|
|
|
body::MessageBody,
|
|
|
|
service::{BoxedHttpServiceFactory, ServiceRequest, ServiceResponse},
|
|
|
|
BoxError, FromRequest, HttpResponse, Responder,
|
2021-04-09 18:07:10 +01:00
|
|
|
};
|
2017-11-03 13:35:34 -07:00
|
|
|
|
2021-04-09 18:07:10 +01: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 16:46:19 -05: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 16:46:19 -05:00
|
|
|
pub trait Handler<T, R>: Clone + 'static
|
2019-03-01 22:51:32 -08:00
|
|
|
where
|
2020-12-26 16:46:19 -05:00
|
|
|
R: Future,
|
|
|
|
R::Output: Responder,
|
2019-03-01 22:51:32 -08:00
|
|
|
{
|
|
|
|
fn call(&self, param: T) -> R;
|
|
|
|
}
|
2018-05-01 17:19:15 -07:00
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
pub(crate) fn handler_service<F, T, R>(handler: F) -> BoxedHttpServiceFactory
|
2020-12-23 23:47:07 +08:00
|
|
|
where
|
2020-12-26 16:46:19 -05:00
|
|
|
F: Handler<T, R>,
|
2020-12-23 23:47:07 +08:00
|
|
|
T: FromRequest,
|
2020-12-26 16:46:19 -05: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 23:47:07 +08:00
|
|
|
{
|
2021-11-17 23:11:35 +03:00
|
|
|
boxed::factory(fn_service(move |req: ServiceRequest| {
|
|
|
|
let handler = handler.clone();
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2021-11-17 23:11:35 +03:00
|
|
|
async move {
|
|
|
|
let (req, mut payload) = req.into_parts();
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2021-11-17 23:11:35 +03: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 23:11:35 +03:00
|
|
|
};
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2021-11-17 23:11:35 +03:00
|
|
|
Ok(ServiceResponse::new(req, res))
|
2019-11-20 23:33:22 +06:00
|
|
|
}
|
2021-11-17 23:11:35 +03:00
|
|
|
}))
|
2018-03-29 15:41:13 -07: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 12:30:53 -04: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 16:46:19 -05:00
|
|
|
Res: Future,
|
|
|
|
Res::Output: Responder,
|
2019-03-01 22:51:32 -08:00
|
|
|
{
|
2021-03-19 12:30:53 -04:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
fn call(&self, ($($param,)*): ($($param,)*)) -> Res {
|
|
|
|
(self)($($param,)*)
|
2019-03-01 22:51:32 -08:00
|
|
|
}
|
2018-03-29 15:41:13 -07:00
|
|
|
}
|
2019-03-01 22:51:32 -08:00
|
|
|
});
|
|
|
|
|
2021-03-19 12:30:53 -04: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 }
|