mirror of https://github.com/fafhrd91/actix-net
Add a MapRequest middleware for preprocessing requests
This commit is contained in:
parent
89e56cf661
commit
2769255b48
|
@ -19,6 +19,7 @@ mod map;
|
||||||
mod map_config;
|
mod map_config;
|
||||||
mod map_err;
|
mod map_err;
|
||||||
mod map_init_err;
|
mod map_init_err;
|
||||||
|
mod map_request;
|
||||||
mod pipeline;
|
mod pipeline;
|
||||||
mod then;
|
mod then;
|
||||||
mod transform;
|
mod transform;
|
||||||
|
@ -28,6 +29,7 @@ pub use self::apply::{apply_fn, apply_fn_factory};
|
||||||
pub use self::apply_cfg::{apply_cfg, apply_cfg_factory};
|
pub use self::apply_cfg::{apply_cfg, apply_cfg_factory};
|
||||||
pub use self::fn_service::{fn_factory, fn_factory_with_config, fn_service};
|
pub use self::fn_service::{fn_factory, fn_factory_with_config, fn_service};
|
||||||
pub use self::map_config::{map_config, unit_config};
|
pub use self::map_config::{map_config, unit_config};
|
||||||
|
pub use self::map_request::{MapRequest, MapRequestMiddleware};
|
||||||
pub use self::pipeline::{pipeline, pipeline_factory, Pipeline, PipelineFactory};
|
pub use self::pipeline::{pipeline, pipeline_factory, Pipeline, PipelineFactory};
|
||||||
pub use self::transform::{apply, Transform};
|
pub use self::transform::{apply, Transform};
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
use std::{cell::RefCell, future::Future, rc::Rc};
|
||||||
|
|
||||||
|
use futures_util::future::{self, FutureExt as _, LocalBoxFuture, TryFutureExt as _};
|
||||||
|
|
||||||
|
use super::{Service, Transform};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct MapRequest<F>(pub F);
|
||||||
|
|
||||||
|
pub struct MapRequestMiddleware<S, F> {
|
||||||
|
service: Rc<RefCell<S>>,
|
||||||
|
f: F,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S, F, R> Transform<S> for MapRequest<F>
|
||||||
|
where
|
||||||
|
S: Service + 'static,
|
||||||
|
F: FnMut(S::Request) -> R + Clone + 'static,
|
||||||
|
R: Future<Output = Result<S::Request, S::Error>> + 'static,
|
||||||
|
{
|
||||||
|
type Request = S::Request;
|
||||||
|
type Response = S::Response;
|
||||||
|
type Error = S::Error;
|
||||||
|
type Transform = MapRequestMiddleware<S, F>;
|
||||||
|
type InitError = ();
|
||||||
|
type Future = future::Ready<Result<Self::Transform, Self::InitError>>;
|
||||||
|
|
||||||
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
|
future::ok(MapRequestMiddleware {
|
||||||
|
service: Rc::new(RefCell::new(service)),
|
||||||
|
f: self.0.clone(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S, F, R> Service for MapRequestMiddleware<S, F>
|
||||||
|
where
|
||||||
|
S: Service + 'static,
|
||||||
|
F: FnMut(S::Request) -> R + Clone + 'static,
|
||||||
|
R: Future<Output = Result<S::Request, S::Error>> + 'static,
|
||||||
|
{
|
||||||
|
type Request = S::Request;
|
||||||
|
type Response = S::Response;
|
||||||
|
type Error = S::Error;
|
||||||
|
type Future = LocalBoxFuture<'static, Result<S::Response, S::Error>>;
|
||||||
|
|
||||||
|
fn poll_ready(
|
||||||
|
&mut self,
|
||||||
|
ctx: &mut std::task::Context<'_>,
|
||||||
|
) -> std::task::Poll<Result<(), Self::Error>> {
|
||||||
|
self.service.borrow_mut().poll_ready(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||||
|
let mut f = self.f.clone();
|
||||||
|
let service = Rc::clone(&self.service);
|
||||||
|
|
||||||
|
f(req)
|
||||||
|
.and_then(move |req| service.borrow_mut().call(req))
|
||||||
|
.boxed_local()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue