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服务器