Files
gen2d/backend/internal/config/config.go
T

36 lines
832 B
Go
Raw Normal View History

2026-05-23 12:19:33 +08:00
// Package config 负责从环境变量加载应用配置。
package config
import (
"os"
"strconv"
)
2026-05-23 12:19:33 +08:00
// Config 应用全局配置,优先读取环境变量,未设置时使用默认值。
type Config struct {
2026-05-23 12:19:33 +08:00
Port int // HTTP 监听端口,默认 8080,环境变量 GEN2D_PORT
Mode string // Gin 运行模式 (debug/release),环境变量 GEN2D_MODE
MaxFileSize int64 // 上传文件大小上限(字节),默认 10MB
}
2026-05-23 12:19:33 +08:00
// Load 从环境变量加载配置并返回。
func Load() *Config {
cfg := &Config{
Port: 8080,
Mode: "debug",
MaxFileSize: 10 << 20, // 10MB
}
if port := os.Getenv("GEN2D_PORT"); port != "" {
if p, err := strconv.Atoi(port); err == nil {
cfg.Port = p
}
}
if mode := os.Getenv("GEN2D_MODE"); mode != "" {
cfg.Mode = mode
}
return cfg
}