优化结构

This commit is contained in:
Yvan
2026-08-24 17:03:46 +08:00
parent acaee94858
commit 7643152c1e
15 changed files with 244 additions and 203 deletions
+20 -3
View File
@@ -63,7 +63,8 @@ func NewStore(config *Config) *Store {
return store
}
// LoadStore loads the initial snapshot and starts watching its source file.
// LoadStore loads the initial snapshot and starts the single process-wide
// Viper-backed watcher.
func LoadStore(path string) (*Store, error) {
config, err := Load(path)
if err != nil {
@@ -260,13 +261,29 @@ func (r *Store) watchLoop(watcher *fsnotify.Watcher, stop <-chan struct{}, done
timerC = timer.C
case <-timerC:
timerC = nil
config, err := Load(path)
next, err := Load(path)
if err != nil {
// Keep the last valid snapshot. A partially-written file must not
// take down a running process or publish invalid state.
continue
}
r.Replace(config)
current := r.Snapshot()
if next.Admin == nil {
next.Admin = &Admin{}
}
// Storage and Email move to database-backed integration settings after
// first initialization. External config file edits must not erase the
// active values when those sections are absent from config.yaml.
if current != nil && current.Admin != nil {
if next.Admin.Storage == nil {
next.Admin.Storage = cloneStorage(current.Admin.Storage)
}
if next.Admin.Email == nil && current.Admin.Email != nil {
email := *current.Admin.Email
next.Admin.Email = &email
}
}
r.Replace(next)
case _, ok := <-errors:
// fsnotify errors are intentionally non-fatal; the watcher remains
// useful for subsequent events and Close always terminates it.