diff options
| author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-10-08 11:58:49 +0200 |
|---|---|---|
| committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-10-08 11:58:49 +0200 |
| commit | 9204e472e4f714c84237bca5ebe740080a589917 (patch) | |
| tree | d17f31240b24fcb2c47eec21edf6a9f512d80123 /crates/rocie-server/src | |
| parent | feat(crates/rocie-server/unit-property): Init (diff) | |
| download | server-9204e472e4f714c84237bca5ebe740080a589917.zip | |
test(crates/rocie-server/testenv/init): Automatically choose the port and wait for server start
This avoids issues regarding a race condition between server start and our start of requests and removes the requirement for specifying free ports in the test files.
Diffstat (limited to 'crates/rocie-server/src')
| -rw-r--r-- | crates/rocie-server/src/cli.rs | 12 | ||||
| -rw-r--r-- | crates/rocie-server/src/main.rs | 22 |
2 files changed, 26 insertions, 8 deletions
diff --git a/crates/rocie-server/src/cli.rs b/crates/rocie-server/src/cli.rs index b2ec214..80b4292 100644 --- a/crates/rocie-server/src/cli.rs +++ b/crates/rocie-server/src/cli.rs @@ -13,8 +13,16 @@ pub(crate) enum Command { /// Serve the server. Serve { /// Which port to serve the server on. - #[arg(short, long, default_value = "8080")] - port: u16, + /// + /// Leave empty to let the OS choose a free one. + #[arg(short, long)] + port: Option<u16>, + + /// Print the used port as single u16 to stdout when started. + /// + /// This can be used, to determine the used port, when the `port` was left at `None`. + #[arg(long)] + print_port: bool, /// Which host to serve the server on. #[arg(short = 'b', long, default_value = "127.0.0.1")] diff --git a/crates/rocie-server/src/main.rs b/crates/rocie-server/src/main.rs index af36c9e..8e10763 100644 --- a/crates/rocie-server/src/main.rs +++ b/crates/rocie-server/src/main.rs @@ -1,3 +1,4 @@ +use actix_cors::Cors; use actix_web::{App, HttpServer, middleware::Logger, web::Data}; use clap::Parser; use utoipa::OpenApi; @@ -49,6 +50,7 @@ async fn main() -> Result<(), std::io::Error> { host, port, db_path, + print_port, } => { let data = Data::new( app::App::new(db_path) @@ -56,10 +58,10 @@ async fn main() -> Result<(), std::io::Error> { .map_err(|err| std::io::Error::other(main::Error::AppInit(err)))?, ); - eprintln!("Serving at http://{host}:{port}"); - - HttpServer::new(move || { + let srv = HttpServer::new(move || { App::new() + // TODO: Remove before an actual deploy <2025-09-26> + .wrap(Cors::permissive()) .wrap(Logger::new( r#"%a "%r" -> %s %b ("%{Referer}i" "%{User-Agent}i" %T s)"#, )) @@ -67,9 +69,17 @@ async fn main() -> Result<(), std::io::Error> { .configure(api::get::register_paths) .configure(api::set::register_paths) }) - .bind((host, port))? - .run() - .await + .bind((host, port.unwrap_or(0)))?; + + let addr = srv.addrs()[0]; + let run = srv.run(); + + if print_port { + println!("{}", addr.port()); + } + eprintln!("Serving at http://{addr}"); + + run.await } Command::OpenApi => { let openapi = ApiDoc::openapi(); |
