feat(环境配置): 支持通过 ENV_FILE 指定不同环境配置文件

增强环境变量加载逻辑,允许通过 ENV_FILE 环境变量指定不同的配置文件
This commit is contained in:
2025-08-28 21:37:21 +08:00
parent 539d13592e
commit 7a010d4c00

View File

@ -15,7 +15,24 @@ use axum::middleware;
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { 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(); tracing_subscriber::fmt().with_env_filter(tracing_subscriber::EnvFilter::from_default_env()).init();
let db = db::init_db().await?; let db = db::init_db().await?;