Function ntex::fn_factory_with_config[][src]

pub fn fn_factory_with_config<F, Fut, Cfg, Srv, Err>(
    f: F
) -> FnServiceConfig<F, Fut, Cfg, Srv, Err> where
    F: Fn(Cfg) -> Fut,
    Fut: Future<Output = Result<Srv, Err>>,
    Srv: Service
Expand description

Create ServiceFactory for function that accepts config argument and can produce services

Any function that has following form Fn(Config) -> Future<Output = Service> could act as a ServiceFactory.

Example

use std::io;
use ntex_service::{fn_factory_with_config, fn_service, Service, ServiceFactory};

#[ntex::main]
async fn main() -> io::Result<()> {
    // Create service factory. factory uses config argument for
    // services it generates.
    let factory = fn_factory_with_config(|y: usize| {
        async move { Ok::<_, io::Error>(fn_service(move |x: usize| async move { Ok::<_, io::Error>(x * y) })) }
    });

    // construct new service with config argument
    let srv = factory.new_service(10).await?;

    let result = srv.call(10).await?;
    assert_eq!(result, 100);

    println!("10 * 10 = {}", result);
    Ok(())
}