use serde::Deserialize;
use std::env;
#[derive(Debug, Deserialize)]
pub struct Settings {
pub database_url: Option<String>,
pub redis_url: Option<String>,
pub dashboard_port: u16,
pub dashboard_host: String,
pub max_workers: u32,
pub max_connections: u32,
pub log_level: String,
pub auto_migrate: bool,
pub require_ssl: bool,
}
impl Settings {
pub fn from_env() -> Result<Self, Box<dyn std::error::Error>> {
#[cfg(feature = "postgres")]
let database_url =
Some(env::var("DATABASE_URL").map_err(|e| format!("DATABASE_URL must be set: {}", e))?);
#[cfg(not(feature = "postgres"))]
let database_url = None;
#[cfg(feature = "redis")]
let redis_url =
Some(env::var("REDIS_URL").map_err(|e| format!("REDIS_URL must be set: {}", e))?);
#[cfg(not(feature = "redis"))]
let redis_url = None;
let dashboard_port = env::var("QML_DASHBOARD_PORT")
.unwrap_or_else(|_| "8080".to_string())
.parse::<u16>()
.map_err(|e| format!("Invalid QML_DASHBOARD_PORT: {}", e))?;
let dashboard_host =
env::var("QML_DASHBOARD_HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
let max_workers = env::var("QML_MAX_WORKERS")
.unwrap_or_else(|_| "10".to_string())
.parse::<u32>()
.map_err(|e| format!("Invalid QML_MAX_WORKERS: {}", e))?;
let max_connections = env::var("QML_MAX_CONNECTIONS")
.unwrap_or_else(|_| "20".to_string())
.parse::<u32>()
.map_err(|e| format!("Invalid QML_MAX_CONNECTIONS: {}", e))?;
let log_level = env::var("QML_LOG_LEVEL").unwrap_or_else(|_| "info".to_string());
let auto_migrate = env::var("QML_AUTO_MIGRATE")
.unwrap_or_else(|_| "true".to_string())
.parse::<bool>()
.map_err(|e| format!("Invalid QML_AUTO_MIGRATE: {}", e))?;
let require_ssl = env::var("QML_REQUIRE_SSL")
.unwrap_or_else(|_| "false".to_string())
.parse::<bool>()
.map_err(|e| format!("Invalid QML_REQUIRE_SSL: {}", e))?;
Ok(Settings {
database_url,
redis_url,
dashboard_port,
dashboard_host,
max_workers,
max_connections,
log_level,
auto_migrate,
require_ssl,
})
}
}