use emailks::{ config::AppConfig, etcd::{EtcdConfig, ServiceRegistry}, EmailksServer, }; use tracing::info; #[tokio::main] async fn main() -> Result<(), Box> { tracing_subscriber::fmt() .with_env_filter( tracing_subscriber::EnvFilter::try_from_default_env() .unwrap_or_else(|_| "info".into()), ) .init(); dotenvy::dotenv().ok(); let etcd_endpoints: Vec = std::env::var("ETCD_ENDPOINTS") .unwrap_or_else(|_| "http://localhost:2379".to_string()) .split(',') .map(|s| s.trim().to_string()) .filter(|s| !s.is_empty()) .collect(); let etcd_prefix = std::env::var("ETCD_KEY_PREFIX") .unwrap_or_else(|_| "/appks/".to_string()); let etcd = EtcdConfig::connect(etcd_endpoints, &etcd_prefix).await?; let listen_addr_str = etcd.get("EMAILKS_LISTEN_ADDR", "127.0.0.1:50051").await; let registry = ServiceRegistry::new(etcd.client(), &etcd_prefix); registry.register("emailks", &listen_addr_str).await?; let smtp_host = etcd.get("APP_SMTP_HOST", "").await; if smtp_host.is_empty() { return Err("APP_SMTP_HOST is required (set via etcd or env)".into()); } let smtp_port: u16 = etcd.get_parsed("APP_SMTP_PORT", 587u16).await; let smtp_from_email = etcd.get("APP_SMTP_FROM_EMAIL", "").await; let smtp_from_name = etcd.get("APP_SMTP_FROM_NAME", "EmailKS").await; let smtp_reply_to = etcd.get("APP_SMTP_REPLY_TO", "").await; let smtp_tls = etcd.get("APP_SMTP_TLS", "starttls").await; let smtp_timeout_secs: u64 = etcd.get_parsed("APP_SMTP_TIMEOUT_SECS", 30u64).await; let smtp_allow_request_from: bool = etcd.get_parsed("APP_SMTP_ALLOW_REQUEST_FROM", false).await; let smtp_username = etcd.get("APP_SMTP_USERNAME", "").await; let smtp_password = etcd.get("APP_SMTP_PASSWORD", "").await; let smtp_helo_name = etcd.get("APP_SMTP_HELO_NAME", "").await; let queue_capacity: Option = { let s = etcd.get("APP_SMTP_QUEUE_CAPACITY", "").await; if s.is_empty() { None } else { s.parse().ok() } }; let config = AppConfig::from_etcd( smtp_host, smtp_port, smtp_username, smtp_password, smtp_from_email, smtp_from_name, smtp_reply_to, smtp_tls, smtp_timeout_secs, smtp_helo_name, smtp_allow_request_from, queue_capacity, &listen_addr_str, )?; info!(host = %config.smtp.host, port = config.smtp.port, "smtp config loaded (etcd priority)"); let server = EmailksServer::builder().config(config).build().await?; server.serve().await?; Ok(()) }