From e4331d13f313f439aff9cef1ea221c8adeeebb3a Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 2 Nov 2026 22:46:48 +0100 Subject: api: allow listening to Unix sockets --- src/main.rs | 43 +++++++++++++------------------------------ 1 file changed, 13 insertions(+), 30 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index c474d45..49971fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,6 @@ //! main.rs only wires things together: arguments, the starting document, the node //! (`node`), bootstrap and the HTTP server. -use std::future::IntoFuture; use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; @@ -98,7 +97,7 @@ async fn main() -> Result<()> { AuthState::from_settings(&args)?.context("htpasswd or user-header is required")?; info!( - bind_addr = args.bind_addr.to_string(), + bind_addr = ?args.bind_addr.iter().map(ToString::to_string).collect::>(), transports = ?args.transports, "Starting replication service" ); @@ -262,23 +261,16 @@ async fn main() -> Result<()> { let joined = AtomicBool::new(false); let app = api::router(state.clone(), auth_state, args.debug_api); - let listener = tokio::net::TcpListener::bind(args.bind_addr) - .await - .with_context(|| format!("Binding local API to {}", args.bind_addr))?; - - info!(address = %listener.local_addr()?, "HTTP API listening"); - - // The node shutting down (`AppState::closing`): the server drains. + // The node shutting down (`AppState::closing`): the servers drain. let closing = state.closing.subscribe(); - // With the address each request came from: a proxy's user header is believed from - // `--trusted-proxies` only (`api::auth`). - let app = app.into_make_service_with_connect_info::(); - let server_builder = axum::serve(listener, app).with_graceful_shutdown(async move { - stopped(closing).await; - info!("Shutdown signal received. Draining API connections...") - }); - - let server_fut = server_builder.into_future(); + let server_fut = api::listen::serve(&args.bind_addr, app, move || { + let closing = closing.clone(); + async move { + stopped(closing).await; + info!("Shutdown signal received. Draining API connections...") + } + }) + .await?; tokio::pin!(server_fut); // Joins, then runs until stopped; returns only if joining fails. @@ -350,19 +342,10 @@ async fn relay_only(args: &config::Settings) -> Result<()> { let server = match auth_state { Some(auth_state) => { let app = api::relay_only_router(Arc::clone(&transport), auth_state); - let listener = tokio::net::TcpListener::bind(args.bind_addr) - .await - .with_context(|| format!("Binding local API to {}", args.bind_addr))?; - info!(address = %listener.local_addr()?, "HTTP API listening"); let stop = stop.clone(); - Some(tokio::spawn(async move { - axum::serve( - listener, - app.into_make_service_with_connect_info::(), - ) - .with_graceful_shutdown(stopped(stop)) - .await - })) + let served = + api::listen::serve(&args.bind_addr, app, move || stopped(stop.clone())).await?; + Some(tokio::spawn(served)) } None => { info!("No htpasswd or user-header: no HTTP API"); -- cgit v1.3.1