fix: launch API server in background

This commit is contained in:
Aravinth Manivannan 2023-10-04 19:04:47 +05:30
parent 501f44560c
commit b3a3fe8c0d
Signed by: realaravinth
GPG key ID: F8F50389936984FF
2 changed files with 14 additions and 11 deletions

View file

@ -47,12 +47,13 @@ semver = { version = "1.0.18", features = ["serde"] }
toml = "0.7.6" toml = "0.7.6"
tokio = { version = "1.32.0", features = ["sync", "time"] } tokio = { version = "1.32.0", features = ["sync", "time"] }
clap = { version = "4.4.6", features = ["derive"] } clap = { version = "4.4.6", features = ["derive"] }
actix-rt = "2.7.0"
[build-dependencies] [build-dependencies]
serde_json = "1" serde_json = "1"
sqlx = { version = "0.6.1", features = [ "runtime-actix-rustls", "postgres", "time", "offline"] } sqlx = { version = "0.6.1", features = [ "runtime-actix-rustls", "postgres", "time", "offline"] }
[dev-dependencies] [dev-dependencies]
actix-rt = "2.7.0" #actix-rt = "2.7.0"
base64 = "0.13.0" base64 = "0.13.0"
mktemp = "0.5.1" mktemp = "0.5.1"

View file

@ -90,7 +90,7 @@ async fn main() -> std::io::Result<()> {
} }
Command::Test { path } => { Command::Test { path } => {
let ctx: Arc<dyn MinAppContext> = Arc::new(CliCtx::new()); let ctx: Arc<dyn MinAppContext> = Arc::new(CliCtx::new());
let serv = basic_server(ctx.clone()).await;
crate::runner::suite::SuiteRunnerState::run_proxy(ctx.as_ref()); crate::runner::suite::SuiteRunnerState::run_proxy(ctx.as_ref());
let (suite_results, init_containers) = let (suite_results, init_containers) =
crate::runner::target::run_target(ctx.as_ref(), path.clone()).await; 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(); std::fs::write(results_file, serde_json::to_string(&content).unwrap()).unwrap();
crate::runner::suite::SuiteRunnerState::stop_proxy(ctx.as_ref()); crate::runner::suite::SuiteRunnerState::stop_proxy(ctx.as_ref());
serv.stop(true).await;
Ok(()) Ok(())
} }
} }
@ -128,14 +129,14 @@ async fn run_daemon() -> std::io::Result<()> {
Ok(()) Ok(())
} }
async fn basic(ctx2: AppMinCtx) -> std::io::Result<()> { async fn basic_server(ctx: Arc<dyn MinAppContext>) -> actix_web::dev::ServerHandle {
let ip = "0.0.0.0:29130"; let ctx = AppMinCtx::new(ctx);
info!("Starting server on: http://{}", ip); info!("Starting server on: http://0.0.0.0:29130");
HttpServer::new(move || { let serv = HttpServer::new(move || {
App::new() App::new()
.wrap(TracingLogger::default()) .wrap(TracingLogger::default())
.wrap(actix_middleware::Compress::default()) .wrap(actix_middleware::Compress::default())
.app_data(ctx2.clone()) .app_data(ctx.clone())
.app_data(get_json_err()) .app_data(get_json_err())
.wrap( .wrap(
actix_middleware::DefaultHeaders::new() actix_middleware::DefaultHeaders::new()
@ -146,11 +147,12 @@ async fn basic(ctx2: AppMinCtx) -> std::io::Result<()> {
)) ))
.configure(services) .configure(services)
}) })
.bind(ip) .bind("0.0.0.0:29130")
.unwrap() .unwrap()
.run() .run();
.await?; let handle = serv.handle();
Ok(()) tokio::spawn(serv);
handle
} }
async fn daemon(ctx: AppFullCtx, ctx2: AppMinCtx) -> std::io::Result<()> { async fn daemon(ctx: AppFullCtx, ctx2: AppMinCtx) -> std::io::Result<()> {