Files
aiops/backend/config/config.go
T
Logan.Li 8df0acb338 fix(k8s): 同步 local 模式数据路径为 /app/sqlite3.db 与二进制同级
- deployment/quick-deploy: PVC 挂载 sqlite3.db/redka.db 到 /app/,含旧 data/ 迁移
- configmap 与 run.sh 状态输出对齐新路径
- README 补充 local 模式说明
2026-06-26 16:19:03 +08:00

106 lines
3.0 KiB
Go

package config
import (
"fmt"
"github.com/spf13/viper"
"go.uber.org/zap"
)
// Config 应用配置
type Config struct {
Mode string `mapstructure:"mode"`
Port string `mapstructure:"port"`
LogLevel string `mapstructure:"log_level"`
Database DatabaseConfig `mapstructure:"database"`
JWT JWTConfig `mapstructure:"jwt"`
Redis RedisConfig `mapstructure:"redis"`
Websocket WebsocketConfig `mapstructure:"websocket"`
}
// DatabaseConfig 数据库配置
type DatabaseConfig struct {
Driver string `mapstructure:"driver"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
DBName string `mapstructure:"dbname"`
Charset string `mapstructure:"charset"`
MaxConn int `mapstructure:"max_conn"`
MaxIdle int `mapstructure:"max_idle"`
}
// JWTConfig JWT配置
type JWTConfig struct {
Secret string `mapstructure:"secret"`
ExpireHour int `mapstructure:"expire_hour"`
}
// RedisConfig Redis配置
type RedisConfig struct {
Driver string `mapstructure:"driver"` // redka (内嵌) / redis (外部)
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Password string `mapstructure:"password"`
DB int `mapstructure:"db"`
}
// WebsocketConfig WebSocket配置
type WebsocketConfig struct {
ReadBufferSize int `mapstructure:"read_buffer_size"`
WriteBufferSize int `mapstructure:"write_buffer_size"`
}
// Load 加载配置
func Load() *Config {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("./configs")
viper.AddConfigPath(".")
// 设置默认值
viper.SetDefault("mode", "development")
viper.SetDefault("port", "8080")
viper.SetDefault("log_level", "info")
viper.SetDefault("database.driver", "sqlite")
viper.SetDefault("database.dbname", "sqlite3.db")
viper.SetDefault("database.host", "localhost")
viper.SetDefault("database.port", 3306)
viper.SetDefault("database.charset", "utf8mb4")
viper.SetDefault("database.max_conn", 100)
viper.SetDefault("database.max_idle", 10)
viper.SetDefault("jwt.expire_hour", 24)
viper.SetDefault("redis.host", "localhost")
viper.SetDefault("redis.port", 6379)
viper.SetDefault("redis.driver", "redka")
viper.SetDefault("websocket.read_buffer_size", 1024)
viper.SetDefault("websocket.write_buffer_size", 1024)
// 读取环境变量
viper.AutomaticEnv()
var cfg Config
if err := viper.ReadInConfig(); err != nil {
zap.L().Warn("config file not found, using defaults", zap.Error(err))
}
if err := viper.Unmarshal(&cfg); err != nil {
zap.L().Fatal("failed to unmarshal config", zap.Error(err))
}
return &cfg
}
// DSN 返回数据库连接字符串
func (d *DatabaseConfig) DSN() string {
if d.Driver == "mysql" {
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local",
d.User, d.Password, d.Host, d.Port, d.DBName, d.Charset)
}
if d.Driver == "sqlite" {
return d.DBName
}
return ""
}