mirror of https://github.com/fafhrd91/actix-web
Merge f5725a85c7
into ce3af777a0
This commit is contained in:
commit
e4711d2469
|
@ -0,0 +1,60 @@
|
|||
//! For middleware documentation, see [`ConditionOption`].
|
||||
|
||||
use futures_core::future::LocalBoxFuture;
|
||||
use futures_util::future::FutureExt as _;
|
||||
|
||||
use crate::{
|
||||
body::EitherBody,
|
||||
dev::{Service, ServiceResponse, Transform},
|
||||
middleware::condition::ConditionMiddleware,
|
||||
};
|
||||
|
||||
/// Middleware for conditionally enabling other middleware in an [`Option`].
|
||||
///
|
||||
/// Uses [`Condition`](crate::middleware::condition::Condition) under the hood.
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// use actix_web::middleware::{ConditionOption, NormalizePath};
|
||||
/// use actix_web::App;
|
||||
///
|
||||
/// let normalize: ConditionOption<_> = Some(NormalizePath::default()).into();
|
||||
/// let app = App::new()
|
||||
/// .wrap(normalize);
|
||||
/// ```
|
||||
pub struct ConditionOption<T>(Option<T>);
|
||||
|
||||
impl<T> From<Option<T>> for ConditionOption<T> {
|
||||
fn from(value: Option<T>) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, T, Req, BE, BD, Err> Transform<S, Req> for ConditionOption<T>
|
||||
where
|
||||
S: Service<Req, Response = ServiceResponse<BD>, Error = Err> + 'static,
|
||||
T: Transform<S, Req, Response = ServiceResponse<BE>, Error = Err>,
|
||||
T::Future: 'static,
|
||||
T::InitError: 'static,
|
||||
T::Transform: 'static,
|
||||
{
|
||||
type Response = ServiceResponse<EitherBody<BE, BD>>;
|
||||
type Error = Err;
|
||||
type Transform = ConditionMiddleware<T::Transform, S>;
|
||||
type InitError = T::InitError;
|
||||
type Future = LocalBoxFuture<'static, Result<Self::Transform, Self::InitError>>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
match &self.0 {
|
||||
Some(transformer) => {
|
||||
let fut = transformer.new_transform(service);
|
||||
async move {
|
||||
let wrapped_svc = fut.await?;
|
||||
Ok(ConditionMiddleware::Enable(wrapped_svc))
|
||||
}
|
||||
.boxed_local()
|
||||
}
|
||||
None => async move { Ok(ConditionMiddleware::Disable(service)) }.boxed_local(),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
mod compat;
|
||||
mod condition;
|
||||
mod condition_option;
|
||||
mod default_headers;
|
||||
mod err_handlers;
|
||||
mod logger;
|
||||
|
@ -11,6 +12,7 @@ mod normalize;
|
|||
|
||||
pub use self::compat::Compat;
|
||||
pub use self::condition::Condition;
|
||||
pub use self::condition_option::ConditionOption;
|
||||
pub use self::default_headers::DefaultHeaders;
|
||||
pub use self::err_handlers::{ErrorHandlerResponse, ErrorHandlers};
|
||||
pub use self::logger::Logger;
|
||||
|
|
Loading…
Reference in New Issue