From d58e9ef1917103c801abcf2608a64d0bd1a8a997 Mon Sep 17 00:00:00 2001 From: Julien Guibert-Peeters Date: Thu, 26 Jun 2025 16:34:04 +0200 Subject: [PATCH] Add env variable to define `beacon` server address This commit replaces the default hardcoded value for the beacon server address with an envvar. This allows one to run comet on one machine while running the UI on another one (e.g. embedded system). --- beacon/src/client.rs | 17 ++++++++++++++--- beacon/src/lib.rs | 6 ++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/beacon/src/client.rs b/beacon/src/client.rs index d381b567..6ad5fd78 100644 --- a/beacon/src/client.rs +++ b/beacon/src/client.rs @@ -15,8 +15,6 @@ use std::sync::Arc; use std::sync::atomic::{self, AtomicBool}; use std::thread; -pub const SERVER_ADDRESS: &str = "127.0.0.1:9167"; - #[derive(Debug, Clone)] pub struct Client { sender: mpsc::Sender, @@ -222,9 +220,22 @@ async fn run( } } +/// Returns the address of the beacon server in this environment. +/// +/// The value of the `ICED_BEACON_SERVER_ADDRESS` env variable will +/// be returned, if defined. +/// +/// Otherwise, a default local server address will be returned. +pub fn server_address_from_env() -> String { + const DEFAULT_ADDRESS: &str = "127.0.0.1:9167"; + + std::env::var("ICED_BEACON_SERVER_ADDRESS") + .unwrap_or_else(|_| String::from(DEFAULT_ADDRESS)) +} + async fn _connect() -> Result { log::debug!("Attempting to connect to server..."); - let stream = net::TcpStream::connect(SERVER_ADDRESS).await?; + let stream = net::TcpStream::connect(server_address_from_env()).await?; stream.set_nodelay(true)?; stream.writable().await?; diff --git a/beacon/src/lib.rs b/beacon/src/lib.rs index b05ea14d..c2d88857 100644 --- a/beacon/src/lib.rs +++ b/beacon/src/lib.rs @@ -91,7 +91,7 @@ impl Event { } pub fn is_running() -> bool { - std::net::TcpListener::bind(client::SERVER_ADDRESS).is_err() + std::net::TcpListener::bind(client::server_address_from_env()).is_err() } pub fn run() -> impl Stream { @@ -99,7 +99,9 @@ pub fn run() -> impl Stream { let mut buffer = Vec::new(); let server = loop { - match net::TcpListener::bind(client::SERVER_ADDRESS).await { + match net::TcpListener::bind(client::server_address_from_env()) + .await + { Ok(server) => break server, Err(error) => { if error.kind() == io::ErrorKind::AddrInUse {