1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-31 19:10:07 +01:00
actix-web/src/handler.rs

76 lines
2.3 KiB
Rust
Raw Permalink Normal View History

2019-11-20 23:33:22 +06:00
use std::future::Future;
2017-10-06 21:48:14 -07:00
2021-11-17 23:11:35 +03:00
use actix_service::{
boxed::{self, BoxServiceFactory},
fn_service,
};
2018-05-01 17:30:06 -07:00
use crate::{
service::{ServiceRequest, ServiceResponse},
2021-11-17 23:11:35 +03:00
Error, FromRequest, HttpResponse, Responder,
};
2017-11-03 13:35:34 -07:00
/// A request handler is an async function that accepts zero or more parameters that can be
/// extracted from a request (i.e., [`impl FromRequest`](crate::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
/// a valid handler. See [Request Handlers](https://actix.rs/docs/handlers/) for more information.
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-11-17 23:11:35 +03:00
pub fn handler_service<F, T, R>(
handler: F,
) -> BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>
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,
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();
async move {
let (req, mut payload) = req.into_parts();
let res = match T::from_request(&req, &mut payload).await {
Err(err) => HttpResponse::from_error(err),
Ok(data) => handler.call(data).await.respond_to(&req),
};
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
}
2019-03-01 22:51:32 -08:00
/// FromRequest trait impl for tuples
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 }