From ffad23446a059b0af54458c5e4fe30cbec80e76b Mon Sep 17 00:00:00 2001 From: ghost Date: Mon, 29 Sep 2025 19:18:53 +0200 Subject: [PATCH] Extract constants and add config loader + example appsettings for niom-signaling; wire main to config --- .gitignore | 2 +- appsettings.example.json | 5 +++++ src/config.rs | 24 ++++++++++++++++++++++++ src/constants.rs | 3 +++ src/main.rs | 21 ++++++++++++++++++--- 5 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 appsettings.example.json create mode 100644 src/config.rs create mode 100644 src/constants.rs diff --git a/.gitignore b/.gitignore index 770d78f..77860e8 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,4 @@ target # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +.idea/ diff --git a/appsettings.example.json b/appsettings.example.json new file mode 100644 index 0000000..b2d9d37 --- /dev/null +++ b/appsettings.example.json @@ -0,0 +1,5 @@ +{ + "server": { + "bind": "127.0.0.1:3478" + } +} diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..ee2f03e --- /dev/null +++ b/src/config.rs @@ -0,0 +1,24 @@ +use serde::Deserialize; +use std::path::Path; + +#[derive(Debug, Deserialize, Clone)] +pub struct ServerOptions { + pub bind: String, +} + +#[derive(Debug, Deserialize, Clone)] +pub struct Config { + pub server: ServerOptions, +} + +impl Config { + pub fn from_file>(p: P) -> Result> { + let s = std::fs::read_to_string(p)?; + let cfg: Config = serde_json::from_str(&s)?; + Ok(cfg) + } + + pub fn load_default() -> Result> { + Self::from_file("appsettings.json") + } +} diff --git a/src/constants.rs b/src/constants.rs new file mode 100644 index 0000000..b639c7a --- /dev/null +++ b/src/constants.rs @@ -0,0 +1,3 @@ +// Central constants for niom-signaling +pub const DEFAULT_BIND: &str = "127.0.0.1:3478"; +pub const DEFAULT_HOST_URL: &str = "http://localhost:3478"; diff --git a/src/main.rs b/src/main.rs index 025d525..bf8c1cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,10 @@ use futures_util::StreamExt; use serde::{Deserialize, Serialize}; use std::sync::Arc; use tokio::sync::broadcast; +mod constants; +mod config; +use crate::config::Config; +use crate::constants::*; #[derive(Clone)] struct AppState { @@ -25,15 +29,26 @@ async fn main() -> std::io::Result<()> { let app_state = AppState { broadcaster: Arc::new(tx) }; + // Try to load config, otherwise fall back to defaults + let cfg = match Config::load_default() { + Ok(c) => { + println!("Loaded config from appsettings.json"); + c + } + Err(_) => { + println!("No config found, using defaults"); + Config { server: crate::config::ServerOptions { bind: DEFAULT_BIND.to_string() } } + } + }; + + println!("Server is running on {}", DEFAULT_HOST_URL); - println!("Server is running on http://localhost:3478"); - HttpServer::new(move || { App::new() .app_data(web::Data::new(app_state.clone())) .route("/ws", web::get().to(websocket_handler)) }) - .bind("127.0.0.1:3478")? + .bind(&cfg.server.bind)? .run() .await }