From b3a3fe8c0d08dde72456c675fb2ef0a3ba4c61c0 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Wed, 4 Oct 2023 19:04:47 +0530 Subject: [PATCH] fix: launch API server in background --- Cargo.toml | 3 ++- src/main.rs | 22 ++++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d83b010..7647b6d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,12 +47,13 @@ semver = { version = "1.0.18", features = ["serde"] } toml = "0.7.6" tokio = { version = "1.32.0", features = ["sync", "time"] } clap = { version = "4.4.6", features = ["derive"] } +actix-rt = "2.7.0" [build-dependencies] serde_json = "1" sqlx = { version = "0.6.1", features = [ "runtime-actix-rustls", "postgres", "time", "offline"] } [dev-dependencies] -actix-rt = "2.7.0" +#actix-rt = "2.7.0" base64 = "0.13.0" mktemp = "0.5.1" diff --git a/src/main.rs b/src/main.rs index 516def3..904affb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -90,7 +90,7 @@ async fn main() -> std::io::Result<()> { } Command::Test { path } => { let ctx: Arc = Arc::new(CliCtx::new()); - + let serv = basic_server(ctx.clone()).await; crate::runner::suite::SuiteRunnerState::run_proxy(ctx.as_ref()); let (suite_results, init_containers) = crate::runner::target::run_target(ctx.as_ref(), path.clone()).await; @@ -104,6 +104,7 @@ async fn main() -> std::io::Result<()> { std::fs::write(results_file, serde_json::to_string(&content).unwrap()).unwrap(); crate::runner::suite::SuiteRunnerState::stop_proxy(ctx.as_ref()); + serv.stop(true).await; Ok(()) } } @@ -128,14 +129,14 @@ async fn run_daemon() -> std::io::Result<()> { Ok(()) } -async fn basic(ctx2: AppMinCtx) -> std::io::Result<()> { - let ip = "0.0.0.0:29130"; - info!("Starting server on: http://{}", ip); - HttpServer::new(move || { +async fn basic_server(ctx: Arc) -> actix_web::dev::ServerHandle { + let ctx = AppMinCtx::new(ctx); + info!("Starting server on: http://0.0.0.0:29130"); + let serv = HttpServer::new(move || { App::new() .wrap(TracingLogger::default()) .wrap(actix_middleware::Compress::default()) - .app_data(ctx2.clone()) + .app_data(ctx.clone()) .app_data(get_json_err()) .wrap( actix_middleware::DefaultHeaders::new() @@ -146,11 +147,12 @@ async fn basic(ctx2: AppMinCtx) -> std::io::Result<()> { )) .configure(services) }) - .bind(ip) + .bind("0.0.0.0:29130") .unwrap() - .run() - .await?; - Ok(()) + .run(); + let handle = serv.handle(); + tokio::spawn(serv); + handle } async fn daemon(ctx: AppFullCtx, ctx2: AppMinCtx) -> std::io::Result<()> {