diff --git a/cmd/safesightd/main.go b/cmd/safesightd/main.go index 05fe142..f835b2b 100644 --- a/cmd/safesightd/main.go +++ b/cmd/safesightd/main.go @@ -6,7 +6,6 @@ import ( "net/http" "os" "path/filepath" - "strconv" "safesight-control/internal/api" "safesight-control/internal/config" @@ -76,14 +75,7 @@ func main() { log.Printf("load persisted tasks: %v", err) } alarmCollector := service.NewAlarmCollector(store.DB(), agentClient, regSvc) - // Load retention from DB (UI-persisted), fallback to config file - retention := cfg.AlarmRetentionDays - if persisted, err := storage.LoadSetting(store.DB(), "alarm_retention_days"); err == nil && persisted != "" { - if v, err := strconv.Atoi(persisted); err == nil && v >= 0 { - retention = v - } - } - alarmCollector.SetRetention(retention) + alarmCollector.SetRetention(cfg.AlarmRetentionDays) alarmCollector.Start() tplSvc := service.NewTemplateService(cfg) h := api.NewHandler(discoSvc, regSvc, agentClient, taskSvc, tplSvc) @@ -114,6 +106,7 @@ func main() { ui.SetStateRepo(stateRepo) ui.SetAuditRepo(auditRepo) ui.SetDBPath(cfg.DBPathOrDefault()) + ui.SetConfig(cfg, cfgPath) ui.SetResourcesRepo(resourcesRepo) ui.SetAlarmCollector(alarmCollector) autoCfgSvc := service.NewAutoConfigService(previewSvc, taskSvc, service.NewFeatureRegistry()) diff --git a/internal/storage/migrate.go b/internal/storage/migrate.go index 19ffd3e..5b6ae87 100644 --- a/internal/storage/migrate.go +++ b/internal/storage/migrate.go @@ -480,10 +480,6 @@ CREATE TABLE IF NOT EXISTS config_versions ( config_id TEXT NOT NULL DEFAULT '', config_json TEXT NOT NULL, created_at TEXT NOT NULL -); -CREATE TABLE IF NOT EXISTS settings ( - key TEXT PRIMARY KEY, - value TEXT NOT NULL DEFAULT '' ) `) return err diff --git a/internal/storage/settings.go b/internal/storage/settings.go deleted file mode 100644 index 2efeb63..0000000 --- a/internal/storage/settings.go +++ /dev/null @@ -1,15 +0,0 @@ -package storage - -import "database/sql" - -func LoadSetting(db *sql.DB, key string) (string, error) { - if db == nil { - return "", nil - } - var value string - err := db.QueryRow("SELECT value FROM settings WHERE key = ?", key).Scan(&value) - if err == sql.ErrNoRows { - return "", nil - } - return value, err -} diff --git a/internal/web/ui.go b/internal/web/ui.go index 53a8305..b7417cb 100644 --- a/internal/web/ui.go +++ b/internal/web/ui.go @@ -23,6 +23,7 @@ import ( "time" "unicode/utf8" + "safesight-control/internal/config" "safesight-control/internal/models" "safesight-control/internal/service" "safesight-control/internal/storage" @@ -40,6 +41,8 @@ type UI struct { stateRepo *storage.DeviceConfigStateRepo auditRepo *storage.AuditLogsRepo dbPath string + configPath string + cfg *config.Config resourcesRepo *storage.ResourcesRepo alarmCollector *service.AlarmCollector autoConfig *service.AutoConfigService @@ -641,6 +644,14 @@ func (u *UI) SetDBPath(path string) { u.dbPath = strings.TrimSpace(path) } +func (u *UI) SetConfig(cfg *config.Config, cfgPath string) { + if u == nil { + return + } + u.cfg = cfg + u.configPath = cfgPath +} + func (u *UI) SetResourcesRepo(repo *storage.ResourcesRepo) { if u == nil { return @@ -4270,8 +4281,8 @@ func (u *UI) renderSystemPage(w http.ResponseWriter, r *http.Request, status int SystemAlarmRetention: 30, SystemConcurrency: 5, } - if u.alarmCollector != nil { - data.SystemAlarmRetention = u.alarmCollector.GetRetention() + if u.cfg != nil { + data.SystemAlarmRetention = u.cfg.AlarmRetentionDays } u.render(w, r, "system", data) } @@ -4283,16 +4294,16 @@ func (u *UI) actionSystemConfig(w http.ResponseWriter, r *http.Request) { u.alarmCollector.SetRetention(days) go u.alarmCollector.RunCleanup() } - // Persist to DB so it survives restart - if strings.TrimSpace(u.dbPath) != "" { - if store, err := storage.OpenSQLite(u.dbPath); err == nil { - store.DB().Exec("INSERT OR REPLACE INTO settings(key,value) VALUES('alarm_retention_days',?)", strconv.Itoa(days)) - store.Close() + // Write back to config file + if u.cfg != nil && strings.TrimSpace(u.configPath) != "" { + u.cfg.AlarmRetentionDays = days + if data, err := json.MarshalIndent(u.cfg, "", " "); err == nil { + os.WriteFile(u.configPath, data, 0o644) } } - msg := "告警保留已设为 " + strconv.Itoa(days) + " 天,已持久化" + msg := "告警保留已设为 " + strconv.Itoa(days) + " 天" if days == 0 { - msg = "告警将永久保留,已持久化" + msg = "告警将永久保留" } u.renderSystemPage(w, r, http.StatusOK, msg, "") return diff --git a/safesightd-linux-arm64 b/safesightd-linux-arm64 index 9be1b35..f8504b7 100644 Binary files a/safesightd-linux-arm64 and b/safesightd-linux-arm64 differ