From 7a010d4c00f44fe1d2d0cabb2bc6a1617628034b Mon Sep 17 00:00:00 2001 From: ayou <550244300@qq.com> Date: Thu, 28 Aug 2025 21:37:21 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=8E=AF=E5=A2=83=E9=85=8D=E7=BD=AE):=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=80=9A=E8=BF=87=20ENV=5FFILE=20=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E4=B8=8D=E5=90=8C=E7=8E=AF=E5=A2=83=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增强环境变量加载逻辑,允许通过 ENV_FILE 环境变量指定不同的配置文件 --- backend/src/main.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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?;