refactor: 系统配置持久化到配置文件而非数据库

This commit is contained in:
tian 2026-07-25 13:30:41 +08:00
parent eb2252f055
commit 2beac76088
5 changed files with 22 additions and 37 deletions

View File

@ -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())

View File

@ -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

View File

@ -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
}

View File

@ -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

Binary file not shown.