pub fn apply_fn_factory<Sf, St, Req, F, In, Out, Err>(
service: impl IntoServiceFactory<Sf, St, Req>,
f: F,
) -> ServiceChainFactory<ApplyFactory<F, Sf, St, Req, In, Out, Err>, St, In>where
Sf: ServiceFactory<St, Req>,
F: AsyncFn(In, &ApplyCtx<'_, <Sf as ServiceFactory<St, Req>>::Service, St, Req>) -> Result<Out, Err> + Clone,
Err: From<<Sf as ServiceFactory<St, Req>>::Error>,Expand description
Applies an asynchronous middleware function to every service from a factory.
Returns a ServiceChainFactory containing an ApplyFactory. Each
successful creation wraps the new inner service with a clone of the
middleware function. The inner factory receives the same state and its
initialization errors are returned unchanged.
The resulting services use the call, binding, readiness, and shutdown
behavior described in apply_fn. To give each created inner service its
own readiness cache, wrap the inner factory with crate::ready_factory().
ยงExample
use ntex_service::{ServiceFactory, apply_fn_factory, fn_factory, fn_service};
let factory = apply_fn_factory(
fn_factory(async |offset: &u32| {
let offset = *offset;
Ok::<_, ()>(fn_service(async move |req: u32| Ok::<_, ()>(req + offset)))
}),
async |req: u8, ctx| ctx.call(u32::from(req)).await,
);
let pipeline = factory.pipeline(10).await?;
assert_eq!(pipeline.call(2).await?, 12);