pub fn fn_factory<F, S, St, Req, Err>(f: F) -> FnFactory<F, S, St, Req, Err>Expand description
Converts an asynchronous constructor function into a ServiceFactory.
Any function with the form
AsyncFn(&State) -> Result<Service, InitError> can act as a factory.
ยงExample
use std::io;
use ntex_service::{factory, fn_factory, fn_service, Pipeline, Service, ServiceFactory};
#[ntex::main]
async fn main() -> io::Result<()> {
// Create a factory that uses the state to configure each service.
let fac = fn_factory(async |y: &usize| {
let y = *y;
Ok::<_, io::Error>(fn_service(move |x: usize| async move { Ok::<_, io::Error>(x * y) }))
});
// Construct a service with the supplied state.
let srv = Pipeline::new((), factory(fac).create(&10).await?);
let result = srv.call(10).await?;
assert_eq!(result, 100);
println!("10 * 10 = {}", result);
Ok(())
}