F1 告警静默规则 [done]: - 后端 alert_silence.go: CRUD + Toggle (onetime/daily/weekly 三模式) - 告警引擎 isSilenced 集成 (alert_engine.go) - 前端 SilenceRuleTab + SilenceDialog - 路由 /alerts/silences 已注册 F2 审计日志导出 [done]: - 重写 ExportAuditLogs: 真正的 CSV 导出 (encoding/csv + UTF-8 BOM) - 支持 module/action/username/startTime/endTime 筛选 - 导出限制 10000 条防溢出,自动记录审计日志 - JSON 格式向后兼容 (format=json) - 前端 exportLogs 传递查询参数 同时包含累积的前期工作: - 目录重构 internal/ -> controller/model/router (v0.55-v0.57) - 后端分层架构 dto/repository/service/utils - 审计模块全功能 (终端审计/操作审计/登录日志) - asciicast v2 录制格式 - SFTP 文件浏览器 - StatStrip 紧凑统计条 - 在线会话自动刷新 - LiveMonitor WebSocket 诊断修复 验证: go build ✅ | npm run build ✅ | Docker 部署 ✅ | 10 回归端点 ✅ | CSV 导出 ✅ | 静默 CRUD ✅
106 lines
3.0 KiB
Go
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", "data/aiops.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 ""
|
|
}
|