Skip to main content

server_with_config

Function server_with_config 

Source
pub fn server_with_config<Cfg, F, I, Sf>(
    cfg: Cfg,
    factory: F,
) -> HttpServer<Cfg, F, I, Sf>
where Cfg: ServerAppConfig, F: AsyncFn(&Cfg::State) -> I + Send + Clone + 'static, I: IntoServiceFactory<Sf, Cfg::State, Request>, Sf: ServiceFactory<Cfg::State, Request> + 'static, Sf::Res: Into<Response>, Sf::Error: ResponseError, Sf::InitError: IntoFailure,
Expand description

Create new http server with application factory and configuration.

use std::io;
use ntex::{web, SharedCfg};

#[derive(Clone)]
struct AppState;

impl web::State for AppState {
    type Error = web::DefaultError;
}

struct AppStateBuilder;

impl ntex::server::ServerAppConfig for AppStateBuilder {
    type State = AppState;

    async fn create(&self) -> io::Result<Self::State> {
        Ok(AppState)
    }
}

#[ntex::main]
async fn main() -> io::Result<()> {
    web::server_with_config(AppStateBuilder, async |_| {
        web::App::new()
            .service(web::resource("/").to(async || { web::HttpResponse::Ok() }))
        })
        .bind("127.0.0.1:59090", SharedCfg::default())?
        .run()
        .await
}