about summary refs log tree commit diff stats
path: root/crates/rocie-server/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rocie-server/src')
-rw-r--r--crates/rocie-server/src/cli.rs12
-rw-r--r--crates/rocie-server/src/main.rs22
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();