use axum::extract::FromRef; use config::{Config, ConfigError, Environment}; use dotenvy::dotenv; use serde::Deserialize; use crate::app_state::AppState; #[derive(Clone, Debug, Deserialize)] pub struct Settings { #[serde(default)] pub base_path: String, pub database_url: String, #[serde(default = "default_host")] pub host: String, #[serde(default = "default_port")] pub port: u16, pub auth: AuthSettings, pub email: EmailSettings, } fn default_port() -> u16 { 3000 } fn default_host() -> String { "127.0.0.1".to_string() } #[derive(Clone, Debug, Deserialize)] pub struct AuthSettings { pub client_id: String, pub client_secret: String, pub redirect_url: String, pub auth_url: String, pub token_url: String, pub userinfo_url: String, pub logout_url: Option, #[serde(default = "default_cookie_name")] pub cookie_name: String, } fn default_cookie_name() -> String { "SHOUT_DOT_DEV_SESSION".to_string() } #[derive(Clone, Debug, Deserialize)] pub struct EmailSettings { pub verification_from: lettre::Address, pub message_from: lettre::Address, pub smtp_server: String, pub smtp_username: String, pub smtp_password: String, } pub struct SlackSettings { pub client_id: String, pub client_secret: String, pub redirect_url: String, pub auth_url: String, pub token_url: String, } impl Settings { pub fn load() -> Result { if let Err(err) = dotenv() { tracing::warn!("Couldn't load .env file: {:?}", err); } let s = Config::builder() .add_source(Environment::default()) .build()?; s.try_deserialize() } } impl FromRef for Settings { fn from_ref(state: &AppState) -> Self { state.settings.clone() } }