fix: 支持 NOFX_BACKEND_PORT 环境变量配置端口 (#764)

- 优先级:环境变量 > 数据库配置 > 默认值 8080
- 修复 .env 中端口配置不生效的问题
- 添加端口来源日志输出
This commit is contained in:
Deloz
2025-11-09 12:21:36 +08:00
committed by GitHub
parent 594116f141
commit 17f4baa6da
+16 -4
View File
@@ -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服务器