Actix-web : Run two sites at different port

actix-web : 1.0.7
actix-server: 0.6.0

I am trying to run two sites at different ports and stuck here.

   Server::build()
    .configure( |cfg : &mut ServiceConfig| {
        cfg.bind( "site1", "0.0.0.0:8000").expect("bind failed");
        cfg.bind( "site2", "0.0.0.0:9000").expect("bind failed");
        cfg.apply(callback);
        Ok(())
    }).expect("Unable to bind")
    .run();


fn callback(runtime : &mut ServiceRuntime)  {
    runtime.service("site1", 
        App::new()
            .service(
                web::resource("/iot/events")
                    .route(web::get().to(outbound))
            )
    );
}

Error :

error[E0271]: type mismatch resolving `<actix_web::app_service::AppInit<actix_web::app_service::AppEntry, actix_http::body::Body> as actix_service::NewService>::Request == actix_server_config::Io<tokio_tcp::stream::TcpStream>`
   |
17 |     runtime.service("site1", App::new()
   |             ^^^^^^^ expected struct `actix_http::request::Request`, found struct `actix_server_config::Io`
   |
   = note: expected type `actix_http::request::Request`
              found type `actix_server_config::Io<tokio_tcp::stream::TcpStream>`

I don't know how to construct a actix_http::request::Request here.

Thank you

Finnaly I figured out how it is done :

fn callback(runtime : &mut ServiceRuntime)  {
    runtime.service("site1",
        HttpService::build()
            .finish(
                App::new()
                    .wrap(middleware::DefaultHeaders::new().header(http::header::CACHE_CONTROL, "no-cache"))
                    //.wrap(middleware::Compress::default())  // <-- enabling this would prevent chunked response.
                    .wrap(middleware::Logger::default())
                    .service(
                        web::resource("/iot/events")
                            .route(web::get().to(outbound))
                            .route(web::put().to_async(inbound))
                    )
            ) 
    );

    runtime.service("site2",
        HttpService::build()
            .finish(
                App::new()
                    .service(
                        web::resource("/")
                            .route(web::get().to(|| HttpResponse::Ok()))
                    )
            ) 
    );
}
    Server::build()
    .configure( |cfg : &mut ServiceConfig| {
        cfg.bind( "site1", "0.0.0.0:8000").expect("bind failed");
        cfg.bind( "site2", "0.0.0.0:9000").expect("bind failed");
        cfg.apply(callback).expect("Failed to configure HTTP service");
        Ok(())
    }).expect("Unable to configure")
    .run().expect("Failed to start HTTP server");
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.