From b8b8feb5fb86357676ce985acdb1cfd6e6a74cf1 Mon Sep 17 00:00:00 2001 From: Deloz Date: Sun, 9 Nov 2025 12:21:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=94=AF=E6=8C=81=20NOFX=5FBACKEND=5FPO?= =?UTF-8?q?RT=20=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3=20(#764)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 优先级:环境变量 > 数据库配置 > 默认值 8080 - 修复 .env 中端口配置不生效的问题 - 添加端口来源日志输出 --- main.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 58bc3dd5..b8b1ed75 100644 --- a/main.go +++ b/main.go @@ -203,7 +203,6 @@ func main() { useDefaultCoins := useDefaultCoinsStr == "true" apiPortStr, _ := database.GetSystemConfig("api_server_port") - // 设置JWT密钥(优先使用环境变量) jwtSecret := strings.TrimSpace(os.Getenv("JWT_SECRET")) if jwtSecret == "" { @@ -317,12 +316,25 @@ func main() { fmt.Println(strings.Repeat("=", 60)) fmt.Println() - // 获取API服务器端口 + // 获取API服务器端口(优先级:环境变量 > 数据库配置 > 默认值) apiPort := 8080 // 默认端口 - if apiPortStr != "" { - if port, err := strconv.Atoi(apiPortStr); err == nil { + + // 1. 优先从环境变量 NOFX_BACKEND_PORT 读取 + if envPort := strings.TrimSpace(os.Getenv("NOFX_BACKEND_PORT")); envPort != "" { + if port, err := strconv.Atoi(envPort); err == nil && port > 0 { apiPort = port + log.Printf("🔌 使用环境变量端口: %d (NOFX_BACKEND_PORT)", apiPort) + } else { + log.Printf("⚠️ 环境变量 NOFX_BACKEND_PORT 无效: %s", envPort) } + } else if apiPortStr != "" { + // 2. 从数据库配置读取(config.json 同步过来的) + if port, err := strconv.Atoi(apiPortStr); err == nil && port > 0 { + apiPort = port + log.Printf("🔌 使用数据库配置端口: %d (api_server_port)", apiPort) + } + } else { + log.Printf("🔌 使用默认端口: %d", apiPort) } // 创建并启动API服务器