use std::path::{Path, PathBuf};
use std::sync::Arc;
use arc_swap::ArcSwap;
use once_cell::sync::Lazy;
use crate::config::{Config, RuntimeModule};
#[derive(Debug, Clone)]
pub struct ModuleSwitches {
pub denoise: bool,
pub vad: bool,
pub eou: bool,
pub ringback_detection: bool,
pub sipflow: bool,
pub playbooks: bool,
}
impl Default for ModuleSwitches {
fn default() -> Self {
Self {
denoise: true,
vad: true,
eou: true,
ringback_detection: true,
sipflow: true,
playbooks: true,
}
}
}
impl ModuleSwitches {
pub fn from_config(config: &Config) -> Self {
Self {
denoise: config.module_enabled(RuntimeModule::Denoise),
vad: config.module_enabled(RuntimeModule::Vad),
eou: config.module_enabled(RuntimeModule::Eou),
ringback_detection: config.module_enabled(RuntimeModule::RingbackDetection),
sipflow: config.module_enabled(RuntimeModule::Sipflow),
playbooks: config.module_enabled(RuntimeModule::Playbooks),
}
}
}
static MODULE_SWITCHES: Lazy<ArcSwap<ModuleSwitches>> =
Lazy::new(|| ArcSwap::from_pointee(ModuleSwitches::default()));
pub fn module_switches() -> Arc<ModuleSwitches> {
MODULE_SWITCHES.load_full()
}
pub fn apply_config(config: &Config) {
MODULE_SWITCHES.store(Arc::new(ModuleSwitches::from_config(config)));
}
pub fn toggle_file_path() -> PathBuf {
std::env::var("PBX_TOGGLE_FILE")
.unwrap_or_else(|_| "/app/.env".to_string())
.into()
}
pub fn toggle_value(key: &str) -> Option<bool> {
toggle_value_from(&toggle_file_path(), key)
}
pub fn toggle_value_from(path: &Path, key: &str) -> Option<bool> {
let content = std::fs::read_to_string(path).ok()?;
let prefix = format!("{}=", key);
for line in content.lines() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}
if let Some(value) = line.strip_prefix(&prefix) {
let value = value.trim().trim_matches('"').to_ascii_lowercase();
return match value.as_str() {
"false" | "0" | "off" => Some(false),
"true" | "1" | "on" => Some(true),
_ => None,
};
}
}
None
}
pub fn module_override(key: &str, configured: bool) -> bool {
toggle_value(key).unwrap_or(configured)
}
#[cfg(test)]
mod tests {
use super::{ModuleSwitches, toggle_value_from};
use crate::config::{AdmissionConfig, Config, ModulesConfig, ModuleToggle, RuntimeModule};
fn temp_toggle_file(name: &str) -> std::path::PathBuf {
let dir = std::env::temp_dir().join(format!(
"ac_runtime_toggle_{}_{}",
std::process::id(),
name
));
std::fs::create_dir_all(&dir).unwrap();
dir.join("app.env")
}
#[test]
fn toggle_file_values() {
let path = temp_toggle_file("values");
std::fs::remove_file(&path).ok();
assert_eq!(toggle_value_from(&path, "DENOISE_ENABLED"), None);
std::fs::write(&path, "RUST_LOG=info\nDENOISE_ENABLED=false\n").unwrap();
assert_eq!(toggle_value_from(&path, "DENOISE_ENABLED"), Some(false));
assert_eq!(toggle_value_from(&path, "EOU_ENABLED"), None);
std::fs::write(&path, "DENOISE_ENABLED=\"on\"\n").unwrap();
assert_eq!(toggle_value_from(&path, "DENOISE_ENABLED"), Some(true));
std::fs::write(&path, "DENOISE_ENABLED=maybe\n").unwrap();
assert_eq!(toggle_value_from(&path, "DENOISE_ENABLED"), None);
}
#[test]
fn modules_come_from_config_with_defaults() {
let mut config = Config::default();
assert!(config.module_enabled(RuntimeModule::Denoise));
config.modules = Some(ModulesConfig {
denoise: Some(ModuleToggle { enabled: false }),
..Default::default()
});
assert!(!config.module_enabled(RuntimeModule::Denoise));
assert!(config.module_enabled(RuntimeModule::Vad));
let switches = ModuleSwitches::from_config(&config);
assert!(!switches.denoise);
assert!(switches.sipflow);
}
#[test]
fn admission_defaults() {
let config = Config::default();
assert_eq!(config.admission_max_calls(), 0);
assert_eq!(config.admission_wait_ms(), 5_000);
let mut config = Config::default();
config.admission = Some(AdmissionConfig {
max_active_calls: Some(120),
wait_ms: Some(1500),
});
assert_eq!(config.admission_max_calls(), 120);
assert_eq!(config.admission_wait_ms(), 1500);
}
}