diff --git a/soul-api/.env b/soul-api/.env index 077a905f..8f654c65 100644 --- a/soul-api/.env +++ b/soul-api/.env @@ -6,7 +6,7 @@ GIN_MODE=debug APP_VERSION=0.0.0 # 数据库(与 Next 现网一致:腾讯云 CDB soul_miniprogram) -DB_DSN=cdb_outerroot:Zhiqun1984@tcp(56b4c23f6853c.gz.cdb.myqcloud.com:14413)/soul_miniprogram?charset=utf8mb4&parseTime=True +DB_DSN=souldev:RXW2FeRcRdH2GtXy@tcp(56b4c23f6853c.gz.cdb.myqcloud.com:14413)/souldev?charset=utf8mb4&parseTime=True # 微信小程序配置 WECHAT_APPID=wxb8bbb2b10dec74aa diff --git a/soul-api/.env.development b/soul-api/.env.development index 077a905f..8f654c65 100644 --- a/soul-api/.env.development +++ b/soul-api/.env.development @@ -6,7 +6,7 @@ GIN_MODE=debug APP_VERSION=0.0.0 # 数据库(与 Next 现网一致:腾讯云 CDB soul_miniprogram) -DB_DSN=cdb_outerroot:Zhiqun1984@tcp(56b4c23f6853c.gz.cdb.myqcloud.com:14413)/soul_miniprogram?charset=utf8mb4&parseTime=True +DB_DSN=souldev:RXW2FeRcRdH2GtXy@tcp(56b4c23f6853c.gz.cdb.myqcloud.com:14413)/souldev?charset=utf8mb4&parseTime=True # 微信小程序配置 WECHAT_APPID=wxb8bbb2b10dec74aa diff --git a/soul-api/internal/config/config.go b/soul-api/internal/config/config.go index 354a1726..37cf9742 100644 --- a/soul-api/internal/config/config.go +++ b/soul-api/internal/config/config.go @@ -75,12 +75,17 @@ func parseCORSOrigins() []string { return origins } -// Load 加载配置,端口等从 .env 读取。优先从可执行文件同目录加载 .env,再试当前目录 +// Load 加载配置,端口等从 env 文件读取。 +// 通过 ENV_FILE 选择:.env.development(默认)或 .env.production;Air 运行时可用 ENV_FILE=.env.production air 切换 func Load() (*Config, error) { - if execPath, err := os.Executable(); err == nil { - _ = godotenv.Load(filepath.Join(filepath.Dir(execPath), ".env")) + envFile := os.Getenv("ENV_FILE") + if envFile == "" { + envFile = ".env.development" } - _ = godotenv.Load(".env") + if execPath, err := os.Executable(); err == nil { + _ = godotenv.Load(filepath.Join(filepath.Dir(execPath), envFile)) + } + _ = godotenv.Load(envFile) port := os.Getenv("PORT") if port == "" {