reface to openteam
This commit is contained in:
241
pkg/config/config.go
Normal file
241
pkg/config/config.go
Normal file
@@ -0,0 +1,241 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
)
|
||||
|
||||
var Cfg *Config
|
||||
|
||||
// Config 结构体存储应用配置
|
||||
type Config struct {
|
||||
// 服务器配置
|
||||
ServerPort int
|
||||
ServerHost string
|
||||
ReadTimeout time.Duration
|
||||
WriteTimeout time.Duration
|
||||
|
||||
// PassKey配置
|
||||
AppName string
|
||||
Domain string
|
||||
AppURL string
|
||||
WebAuthnTimeout time.Duration
|
||||
ChallengeExpiration time.Duration
|
||||
|
||||
// 数据库配置
|
||||
DB_Type string
|
||||
DSN string
|
||||
DBMaxOpenConns int
|
||||
DBMaxIdleConns int
|
||||
// DBHost string
|
||||
// DBPort int
|
||||
// DBUser string
|
||||
// DBPassword string
|
||||
// DBName string
|
||||
|
||||
// 缓存配置
|
||||
RedisHost string
|
||||
RedisPort int
|
||||
RedisPassword string
|
||||
RedisDB int
|
||||
|
||||
// 日志配置
|
||||
LogLevel string
|
||||
LogPath string
|
||||
|
||||
// 其他应用特定配置
|
||||
AllowRegister bool
|
||||
UnlimitedQuota bool
|
||||
DefaultActive bool
|
||||
|
||||
UsageWorker int
|
||||
UsageChanSize int
|
||||
|
||||
TaskTimeInterval int
|
||||
}
|
||||
|
||||
func init() {
|
||||
// 加载配置
|
||||
cfg, err := LoadConfig()
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("加载配置失败: %v", err))
|
||||
}
|
||||
Cfg = cfg
|
||||
}
|
||||
|
||||
// LoadConfig 从环境变量加载配置
|
||||
func LoadConfig() (*Config, error) {
|
||||
cfg := &Config{
|
||||
AppName: "OpenTeam",
|
||||
Domain: "localhost",
|
||||
AppURL: "https://localhost:5173",
|
||||
// 默认值设置
|
||||
ServerPort: 8080,
|
||||
ServerHost: "0.0.0.0",
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
|
||||
LogLevel: "info",
|
||||
LogPath: "./logs/",
|
||||
|
||||
DB_Type: "sqlite",
|
||||
DSN: "",
|
||||
DBMaxOpenConns: 10,
|
||||
DBMaxIdleConns: 5,
|
||||
|
||||
RedisDB: 0,
|
||||
|
||||
// 系统设置
|
||||
AllowRegister: false,
|
||||
UnlimitedQuota: false,
|
||||
DefaultActive: false,
|
||||
|
||||
UsageWorker: 1,
|
||||
UsageChanSize: 1000,
|
||||
TaskTimeInterval: 60,
|
||||
}
|
||||
|
||||
// PassKey配置
|
||||
if appName := os.Getenv("APP_NAME"); appName != "" {
|
||||
cfg.AppName = appName
|
||||
}
|
||||
if domain := os.Getenv("DOMAIN"); domain != "" {
|
||||
cfg.Domain = domain
|
||||
}
|
||||
if appURL := os.Getenv("APP_URL"); appURL != "" {
|
||||
cfg.AppURL = appURL
|
||||
}
|
||||
|
||||
// 服务器配置
|
||||
if port := os.Getenv("SERVER_PORT"); port != "" {
|
||||
if p, err := strconv.Atoi(port); err == nil {
|
||||
cfg.ServerPort = p
|
||||
} else {
|
||||
return nil, fmt.Errorf("无效的SERVER_PORT: %s", port)
|
||||
}
|
||||
}
|
||||
|
||||
if host := os.Getenv("SERVER_HOST"); host != "" {
|
||||
cfg.ServerHost = host
|
||||
}
|
||||
|
||||
if timeout := os.Getenv("READ_TIMEOUT"); timeout != "" {
|
||||
if t, err := strconv.Atoi(timeout); err == nil {
|
||||
cfg.ReadTimeout = time.Duration(t) * time.Second
|
||||
} else {
|
||||
return nil, fmt.Errorf("无效的READ_TIMEOUT: %s", timeout)
|
||||
}
|
||||
}
|
||||
|
||||
if timeout := os.Getenv("WRITE_TIMEOUT"); timeout != "" {
|
||||
if t, err := strconv.Atoi(timeout); err == nil {
|
||||
cfg.WriteTimeout = time.Duration(t) * time.Second
|
||||
} else {
|
||||
return nil, fmt.Errorf("无效的WRITE_TIMEOUT: %s", timeout)
|
||||
}
|
||||
}
|
||||
|
||||
// 数据库配置
|
||||
if dbType := os.Getenv("DB_TYPE"); dbType != "" {
|
||||
cfg.DB_Type = dbType
|
||||
} else {
|
||||
cfg.DB_Type = "sqlite"
|
||||
}
|
||||
|
||||
if dsn := os.Getenv("DB_DSN"); dsn != "" {
|
||||
cfg.DSN = dsn
|
||||
}
|
||||
|
||||
if conns := os.Getenv("DB_MAX_OPEN_CONNS"); conns != "" {
|
||||
if c, err := strconv.Atoi(conns); err == nil {
|
||||
cfg.DBMaxOpenConns = c
|
||||
} else {
|
||||
return nil, fmt.Errorf("无效的DB_MAX_OPEN_CONNS: %s", conns)
|
||||
}
|
||||
}
|
||||
|
||||
if conns := os.Getenv("DB_MAX_IDLE_CONNS"); conns != "" {
|
||||
if c, err := strconv.Atoi(conns); err == nil {
|
||||
cfg.DBMaxIdleConns = c
|
||||
} else {
|
||||
return nil, fmt.Errorf("无效的DB_MAX_IDLE_CONNS: %s", conns)
|
||||
}
|
||||
}
|
||||
|
||||
// Redis配置
|
||||
if host := os.Getenv("REDIS_HOST"); host != "" {
|
||||
cfg.RedisHost = host
|
||||
}
|
||||
|
||||
if port := os.Getenv("REDIS_PORT"); port != "" {
|
||||
if p, err := strconv.Atoi(port); err == nil {
|
||||
cfg.RedisPort = p
|
||||
} else {
|
||||
return nil, fmt.Errorf("无效的REDIS_PORT: %s", port)
|
||||
}
|
||||
}
|
||||
|
||||
if password := os.Getenv("REDIS_PASSWORD"); password != "" {
|
||||
cfg.RedisPassword = password
|
||||
}
|
||||
|
||||
if db := os.Getenv("REDIS_DB"); db != "" {
|
||||
if d, err := strconv.Atoi(db); err == nil {
|
||||
cfg.RedisDB = d
|
||||
} else {
|
||||
return nil, fmt.Errorf("无效的REDIS_DB: %s", db)
|
||||
}
|
||||
}
|
||||
|
||||
// 日志配置
|
||||
if level := os.Getenv("LOG_LEVEL"); level != "" {
|
||||
cfg.LogLevel = level
|
||||
}
|
||||
|
||||
if path := os.Getenv("LOG_PATH"); path != "" {
|
||||
cfg.LogPath = path
|
||||
}
|
||||
|
||||
// 功能标志
|
||||
if allowRegister := os.Getenv("ALLOW_REGISTER"); allowRegister != "" {
|
||||
if b, err := strconv.ParseBool(allowRegister); err == nil {
|
||||
cfg.AllowRegister = b
|
||||
}
|
||||
}
|
||||
|
||||
if unlimitedQuota := os.Getenv("UNLIMITED_QUOTA"); unlimitedQuota != "" {
|
||||
if b, err := strconv.ParseBool(unlimitedQuota); err == nil {
|
||||
cfg.UnlimitedQuota = b
|
||||
}
|
||||
}
|
||||
|
||||
if defaultActive := os.Getenv("DEFAULT_ACTIVE"); defaultActive != "" {
|
||||
if b, err := strconv.ParseBool(defaultActive); err == nil {
|
||||
cfg.DefaultActive = b
|
||||
}
|
||||
}
|
||||
|
||||
if worker := os.Getenv("USAGE_WORKER"); worker != "" {
|
||||
if w, err := strconv.Atoi(worker); err == nil {
|
||||
cfg.UsageWorker = w
|
||||
}
|
||||
}
|
||||
|
||||
if size := os.Getenv("USAGE_CHAN_SIZE"); size != "" {
|
||||
if s, err := strconv.Atoi(size); err == nil {
|
||||
cfg.UsageChanSize = s
|
||||
}
|
||||
}
|
||||
|
||||
if interval := os.Getenv("TASK_TIME_INTERVAL"); interval != "" {
|
||||
if i, err := strconv.Atoi(interval); err == nil {
|
||||
cfg.TaskTimeInterval = i
|
||||
}
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
Reference in New Issue
Block a user