Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run multiple actix app on different ports

I am trying to run two app (one to admin on port 3006 and another to serve data on port 8080).
They shared database pool, cache...

For actix 1.0 i had this working (i don't know if it's the best way to do that) :

let server = Server::build()
  // FIRST APP
  .bind("", "0.0.0.0:3006", move || {
      HttpService::build().finish({
          App::new()
              .wrap(actix_Logger::new(
                  "WMTS %a %r %s %b %{Referer}i %{User-Agent}i %T",
              ))
              .data(pool.clone())
              .data(cache.clone())
              .service(
                  web::scope("/utils")
                      .route(
                          "one",
                          web::get().to(api::one),
                      )
                      .route("two", web::get().to(api::two))
              )
              .service(
                  web::scope("/data")
                      .route("get_data", web::get().to(api::get_data)),
              )
      })
  })
  .unwrap()
  // SECOND APP
  .bind("", "0.0.0.0:8080", move || {
      HttpService::build().finish(
          App::new()
              .wrap(actix_Logger::new(
                  "API %a %r %s %b %{Referer}i %{User-Agent}i %T",
              ))
              .data(pool.clone())
              .data(cache.clone())
              .service(web::resource("/graphql").route(web::post().to(api::graphql)))
              .service(web::resource("/health").route(web::get().to(api::health)))
              .service(web::resource("/metrics").route(web::get().to(api::metrics))),
      )
  })
  .unwrap();

  server.run()?;

But how to make it work with actix 2.0 ?

like image 481
Etienne Prothon Avatar asked Jan 08 '20 09:01

Etienne Prothon


1 Answers

As far as actix-web's own API is concerned, there really isn't much changed between 1.0 and 2.0. That's a good thing since you still have the familiar API at your disposal to configure routes, application data, logger, etc.

One thing that does change is actix-web has moved to async / await. Your application needs to adapt to it as well:

//# actix-rt = "1.0"
//# actix-web = "2.0"
//# futures = "0.3"
use actix_web::{web, App, HttpServer, Responder};
use futures::future;

async fn utils_one() -> impl Responder {
    "Utils one reached\n"
}

async fn health() -> impl Responder {
    "All good\n"
}

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
    let s1 = HttpServer::new(move || {
            App::new().service(web::scope("/utils").route("/one", web::get().to(utils_one)))
        })
        .bind("0.0.0.0:3006")?
        .run();
    let s2 = HttpServer::new(move || {
            App::new().service(web::resource("/health").route(web::get().to(health)))
        })
        .bind("0.0.0.0:8080")?
        .run();
    future::try_join(s1, s2).await?;

    Ok(())
}

I suppose you can still use Server::build API to build up multiple bindings, but the approach showed here is more modular. HttpServer::run just returns a Future now. You then join the two and await on both of them.

It works as expected:

$ curl http://127.0.0.1:3006/utils/one
Utils one reached
$ curl http://127.0.0.1:8080/health
All good
like image 165
edwardw Avatar answered Oct 07 '22 01:10

edwardw