feat: mode=local 统一走 SQLite + redka 本地存储
- 二进制 --mode local 或环境变量 MODE=local 自动配置 SQLite + redka - K8s Deployment 默认 MODE=local,移除外部 MySQL/Redis 依赖 - /version 返回 redis_driver;SQLite 兼容 agent 数据清理 SQL
This commit is contained in:
1 parent
ec7aa61948
commit
1f4f63cf6e
4 files changed
+78
-54
No files matched your search
+50
-26
@@ -34,7 +34,7 @@ var (
|
||||
var (
|
||||
flagPort = flag.String("port", "", "服务端口 (默认: 8080)")
|
||||
flagConfig = flag.String("config", "", "配置文件路径 (默认: config.yaml)")
|
||||
flagMode = flag.String("mode", "", "运行模式: development, production, release")
|
||||
flagMode = flag.String("mode", "", "运行模式: local (SQLite+redka), development, production, release")
|
||||
flagLogLevel = flag.String("log-level", "", "日志级别: debug, info, warn, error")
|
||||
|
||||
// 数据库参数
|
||||
@@ -107,9 +107,12 @@ func main() {
|
||||
zap.String("port", cfg.Port),
|
||||
)
|
||||
|
||||
// 设置Gin模式
|
||||
if cfg.Mode == "production" || cfg.Mode == "release" {
|
||||
// 设置 Gin 模式(local 与 release 一样走 ReleaseMode)// Logan.Li
|
||||
switch cfg.Mode {
|
||||
case "production", "release", "local":
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
default:
|
||||
gin.SetMode(gin.DebugMode)
|
||||
}
|
||||
|
||||
// 初始化数据库
|
||||
@@ -154,25 +157,31 @@ func main() {
|
||||
// 版本信息 API
|
||||
r.GET("/version", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"version": Version,
|
||||
"build_time": BuildTime,
|
||||
"git_commit": GitCommit,
|
||||
"mode": cfg.Mode,
|
||||
"db_driver": cfg.Database.Driver,
|
||||
"version": Version,
|
||||
"build_time": BuildTime,
|
||||
"git_commit": GitCommit,
|
||||
"mode": cfg.Mode,
|
||||
"db_driver": cfg.Database.Driver,
|
||||
"redis_driver": cfg.Redis.Driver,
|
||||
})
|
||||
})
|
||||
|
||||
// 注册路由
|
||||
router.Register(r, db, log, cfg, cache)
|
||||
|
||||
// 定期清理旧的 agent 上报数据(保留 7 天,防止表膨胀影响查询性能)
|
||||
// 定期清理旧的 agent 上报数据(保留 7 天,防止表膨胀影响查询性能)// Logan.Li
|
||||
go func() {
|
||||
ticker := time.NewTicker(24 * time.Hour)
|
||||
defer ticker.Stop()
|
||||
for range ticker.C {
|
||||
result := db.Exec("DELETE FROM monitor_agent_reports WHERE report_time < DATE_SUB(NOW(), INTERVAL 7 DAY)")
|
||||
if result.RowsAffected > 0 {
|
||||
log.Info("清理过期的 agent 上报数据", zap.Int64("rows", result.RowsAffected))
|
||||
var rows int64
|
||||
if cfg.Database.Driver == "sqlite" {
|
||||
rows = db.Exec("DELETE FROM monitor_agent_reports WHERE report_time < datetime('now', '-7 days')").RowsAffected
|
||||
} else {
|
||||
rows = db.Exec("DELETE FROM monitor_agent_reports WHERE report_time < DATE_SUB(NOW(), INTERVAL 7 DAY)").RowsAffected
|
||||
}
|
||||
if rows > 0 {
|
||||
log.Info("清理过期的 agent 上报数据", zap.Int64("rows", rows))
|
||||
}
|
||||
}
|
||||
}()
|
||||
@@ -265,20 +274,8 @@ func loadConfig() *config.Config {
|
||||
// CLI flags 覆盖(最高优先级)// Logan.Li
|
||||
applyFlags()
|
||||
|
||||
// --mode local 快捷模式:自动配置 SQLite + redka 本地缓存 // Logan.Li
|
||||
if *flagMode == "local" || viper.GetString("mode") == "local" {
|
||||
// 数据库:SQLite,文件名 sqlite3.db
|
||||
if *flagDBDriver == "" {
|
||||
viper.Set("database.driver", "sqlite")
|
||||
}
|
||||
if *flagDBName == "" {
|
||||
viper.Set("database.dbname", "data/sqlite3.db")
|
||||
}
|
||||
// 缓存:redka 本地内嵌(无需外部 Redis)
|
||||
if *flagRedisDriver == "" {
|
||||
viper.Set("redis.driver", "redka")
|
||||
}
|
||||
}
|
||||
// mode=local:强制 SQLite + redka(CLI 显式传 --db-driver/--redis-driver 时可覆盖)// Logan.Li
|
||||
applyLocalMode()
|
||||
|
||||
// 解析配置 // Logan.Li
|
||||
var cfg config.Config
|
||||
@@ -290,6 +287,33 @@ func loadConfig() *config.Config {
|
||||
return &cfg
|
||||
}
|
||||
|
||||
// isFlagSet 判断 CLI 参数是否被显式传入 // Logan.Li
|
||||
func isFlagSet(name string) bool {
|
||||
found := false
|
||||
flag.Visit(func(f *flag.Flag) {
|
||||
if f.Name == name {
|
||||
found = true
|
||||
}
|
||||
})
|
||||
return found
|
||||
}
|
||||
|
||||
// applyLocalMode local 模式:不依赖外部 MySQL/Redis,使用 SQLite + redka // Logan.Li
|
||||
func applyLocalMode() {
|
||||
if viper.GetString("mode") != "local" {
|
||||
return
|
||||
}
|
||||
if !isFlagSet("db-driver") {
|
||||
viper.Set("database.driver", "sqlite")
|
||||
}
|
||||
if !isFlagSet("db-name") {
|
||||
viper.Set("database.dbname", "data/sqlite3.db")
|
||||
}
|
||||
if !isFlagSet("redis-driver") {
|
||||
viper.Set("redis.driver", "redka")
|
||||
}
|
||||
}
|
||||
|
||||
// applyFlags 将 CLI 参数映射到 viper(覆盖 config.yaml 和默认值)// Logan.Li
|
||||
func applyFlags() {
|
||||
// 检查是否有任何 flag 被显式设置 // Logan.Li
|
||||
|
||||
Reference in new issue
Block a user