Files
opsx-licence/backend/config/config.go
T
Logan.LiandCursor 95405de119 feat: 对齐 Waffo 包年/永久结账与客户端席位 API
同步 OpsXShell 档位定价与 Waffo 商品映射,补齐 checkout/webhook/设备列表契约,便于桌面端购买与满席释放。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-12 11:42:56 +08:00

103 lines
2.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package config
import (
"crypto/rand"
"encoding/base64"
"log"
"os"
"strings"
"github.com/joho/godotenv"
)
// Config 应用配置 // Logan.Li
type Config struct {
// 服务器配置
Port string
Mode string // debug, release
// 数据库配置
DBDriver string
DBHost string
DBPort string
DBUser string
DBPassword string
DBName string
// JWT 配置
JWTSecret string
// CORS 配置
CORSOrigins []string
}
// Load 从环境变量加载配置 // Logan.Li
func Load() *Config {
// 加载 .env:backend/.env 与仓库根 .env 各自尝试(缺文件不中断) // Logan.Li
_ = godotenv.Load(".env")
_ = godotenv.Load("../.env")
_ = godotenv.Load("../../.env")
cfg := &Config{
Port: getEnv("PORT", "9001"),
Mode: getEnv("GIN_MODE", "debug"),
DBDriver: getEnv("DB_DRIVER", "sqlite"),
DBHost: getEnv("DB_HOST", "127.0.0.1"),
DBPort: getEnv("DB_PORT", "3306"),
DBUser: getEnv("DB_USER", "root"),
DBPassword: getEnv("DB_PASSWORD", ""),
DBName: getEnv("DB_NAME", "db.sqlite3"),
JWTSecret: getEnv("JWT_SECRET", ""),
CORSOrigins: []string{
"http://localhost:5181",
"http://127.0.0.1:5181",
"http://localhost:3000",
"http://127.0.0.1:3000",
// OpsXShell Tauri / Vite dev origins
"http://localhost:1420",
"http://127.0.0.1:1420",
"http://tauri.localhost",
"https://tauri.localhost",
"tauri://localhost",
},
}
// 安全检查:生产环境拒绝空密钥,开发环境使用进程级随机密钥避免固定后门。 // Logan.Li
if cfg.JWTSecret == "" {
if cfg.Mode == "release" {
log.Fatal("[安全] 生产环境必须设置 JWT_SECRET 环境变量")
}
cfg.JWTSecret = generateEphemeralJWTSecret()
log.Println("[安全] JWT_SECRET 未设置,已生成仅当前开发进程有效的随机密钥")
}
return cfg
}
// IsOriginAllowed 判断浏览器 Origin 是否在显式 CORS 白名单中。 // Logan.Li
func (c *Config) IsOriginAllowed(origin string) bool {
for _, allowed := range c.CORSOrigins {
if strings.EqualFold(strings.TrimSpace(origin), strings.TrimSpace(allowed)) {
return true
}
}
return false
}
// generateEphemeralJWTSecret 生成开发环境临时 JWT 密钥。 // Logan.Li
func generateEphemeralJWTSecret() string {
bytes := make([]byte, 32)
if _, err := rand.Read(bytes); err != nil {
log.Fatal("[安全] 生成开发 JWT 密钥失败")
}
return base64.RawURLEncoding.EncodeToString(bytes)
}
// getEnv 读取环境变量,不存在则返回默认值 // Logan.Li
func getEnv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}