Wire appsettings.json Config into main: bind address and credential store population
This commit is contained in:
parent
cd2462915a
commit
c427dd4bb6
40
src/config.rs
Normal file
40
src/config.rs
Normal file
@ -0,0 +1,40 @@
|
||||
use serde::Deserialize;
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct CredentialEntry {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct ServerOptions {
|
||||
/// Listen address, e.g. "0.0.0.0:3478"
|
||||
pub bind: String,
|
||||
/// Optional TLS: cert/key paths (not used in MVP)
|
||||
pub tls_cert: Option<String>,
|
||||
pub tls_key: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct Config {
|
||||
/// Server options
|
||||
pub server: ServerOptions,
|
||||
/// Initial credentials to populate the credential store
|
||||
#[serde(default)]
|
||||
pub credentials: Vec<CredentialEntry>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/// Load configuration from a JSON file path
|
||||
pub fn from_file<P: AsRef<Path>>(p: P) -> anyhow::Result<Self> {
|
||||
let s = std::fs::read_to_string(p)?;
|
||||
let cfg: Config = serde_json::from_str(&s)?;
|
||||
Ok(cfg)
|
||||
}
|
||||
|
||||
/// Try to load `appsettings.json` from current working directory as a convenience
|
||||
pub fn load_default() -> anyhow::Result<Self> {
|
||||
Self::from_file("appsettings.json")
|
||||
}
|
||||
}
|
||||
@ -5,6 +5,7 @@ pub mod auth;
|
||||
pub mod traits;
|
||||
pub mod models;
|
||||
pub mod alloc;
|
||||
pub mod config;
|
||||
|
||||
pub use crate::auth::*;
|
||||
pub use crate::stun::*;
|
||||
|
||||
30
src/main.rs
30
src/main.rs
@ -9,6 +9,7 @@ use niom_turn::auth::InMemoryStore;
|
||||
use niom_turn::stun::{parse_message, build_401_response, find_message_integrity, validate_message_integrity, build_success_response, encode_xor_relayed_address};
|
||||
use niom_turn::traits::CredentialStore;
|
||||
use niom_turn::alloc::AllocationManager;
|
||||
use niom_turn::config::Config;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
@ -16,12 +17,33 @@ async fn main() -> anyhow::Result<()> {
|
||||
|
||||
info!("niom-turn starting");
|
||||
|
||||
// config
|
||||
let bind_addr: SocketAddr = "0.0.0.0:3478".parse()?;
|
||||
// config: try to load appsettings.json, otherwise fall back to defaults
|
||||
let cfg = match Config::load_default() {
|
||||
Ok(c) => {
|
||||
info!("loaded config from appsettings.json");
|
||||
c
|
||||
}
|
||||
Err(e) => {
|
||||
info!("no appsettings.json found or failed to load: {} — using defaults", e);
|
||||
// defaults
|
||||
Config {
|
||||
server: niom_turn::config::ServerOptions {
|
||||
bind: "0.0.0.0:3478".to_string(),
|
||||
tls_cert: None,
|
||||
tls_key: None,
|
||||
},
|
||||
credentials: vec![niom_turn::config::CredentialEntry { username: "testuser".into(), password: "secretpassword".into() }],
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Initialize credential store (MVP demo user)
|
||||
let bind_addr: SocketAddr = cfg.server.bind.parse()?;
|
||||
|
||||
// Initialize credential store and populate from config
|
||||
let creds = InMemoryStore::new();
|
||||
creds.insert("testuser", "secretpassword");
|
||||
for c in cfg.credentials.iter() {
|
||||
creds.insert(&c.username, &c.password);
|
||||
}
|
||||
|
||||
// UDP listener for TURN/STUN
|
||||
let udp = UdpSocket::bind(bind_addr).await?;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user