diff --git a/backend/src/main.rs b/backend/src/main.rs index 26d80e8..d62b413 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -15,7 +15,24 @@ use axum::middleware; #[tokio::main] async fn main() -> anyhow::Result<()> { - dotenvy::dotenv().ok(); + // 增强:支持通过 ENV_FILE 指定要加载的环境文件 + // - ENV_FILE=prod 或 production => .env.prod + // - ENV_FILE=dev 或 development => .env + // - ENV_FILE=staging => .env.staging + // - ENV_FILE=任意字符串 => 视为显式文件名或路径 + if let Ok(v) = std::env::var("ENV_FILE") { + let filename = match v.trim() { + "" => ".env".to_string(), + "prod" | "production" => ".env.prod".to_string(), + "dev" | "development" => ".env".to_string(), + "staging" | "pre" | "preprod" | "pre-production" => ".env.staging".to_string(), + other => other.to_string(), + }; + dotenvy::from_filename(&filename).ok(); + } else { + dotenvy::dotenv().ok(); + } + tracing_subscriber::fmt().with_env_filter(tracing_subscriber::EnvFilter::from_default_env()).init(); let db = db::init_db().await?;