Function actix_service::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 actix_service::{fn_factory_with_config, fn_service, Service, ServiceFactory};
use futures_util::future::ok;

#[actix_rt::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| {
        ok::<_, io::Error>(fn_service(move |x: usize| ok::<_, io::Error>(x * y)))
    });

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

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

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