refactor: 使用 Viper 重构配置管理并实现生产级热重载
- 引入 Viper 库替代手动 YAML 解析 - 实现基于 fsnotify 的配置文件热重载 - 使用 atomic.Value 保证并发安全的配置读写 - 添加配置验证(必填字段检查) - 深拷贝 Hotwords 切片防止数据竞争 - 使用绝对路径匹配提升跨平台可靠性 - 支持启动失败后重试(不锁死状态) - 提供 stop 函数正确清理 watcher 资源 - 通过 Oracle 多轮审计确认生产就绪
This commit is contained in:
@@ -1,9 +1,12 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
@@ -47,6 +50,13 @@ func defaults() Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// global holds the current config atomically for concurrent reads.
|
||||||
|
var global atomic.Value
|
||||||
|
var watcher *fsnotify.Watcher
|
||||||
|
var watcherMu sync.Mutex
|
||||||
|
var watchStarted bool
|
||||||
|
var watchStartErr error
|
||||||
|
|
||||||
// Load reads config from file (or uses defaults if file doesn't exist).
|
// Load reads config from file (or uses defaults if file doesn't exist).
|
||||||
// Empty path defaults to "config.yaml".
|
// Empty path defaults to "config.yaml".
|
||||||
func Load(path string) (Config, error) {
|
func Load(path string) (Config, error) {
|
||||||
@@ -81,54 +91,176 @@ func Load(path string) (Config, error) {
|
|||||||
if err := v.Unmarshal(&cfg); err != nil {
|
if err := v.Unmarshal(&cfg); err != nil {
|
||||||
return Config{}, err
|
return Config{}, err
|
||||||
}
|
}
|
||||||
|
// Validate before storing
|
||||||
|
if err := validate(cfg); err != nil {
|
||||||
|
return Config{}, err
|
||||||
|
}
|
||||||
|
store(cfg)
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
// validate checks required fields.
|
||||||
|
func validate(cfg Config) error {
|
||||||
|
if cfg.Doubao.AppID == "" {
|
||||||
|
return fmt.Errorf("doubao.app_id is required")
|
||||||
|
}
|
||||||
|
if cfg.Doubao.AccessToken == "" {
|
||||||
|
return fmt.Errorf("doubao.access_token is required")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// WatchAndReload starts watching config file for changes and reloads automatically.
|
// WatchAndReload starts watching config file for changes and reloads automatically.
|
||||||
// Empty path defaults to "config.yaml".
|
// Empty path defaults to "config.yaml".
|
||||||
func WatchAndReload(path string) {
|
// Returns a function to stop watching. Can only be called once.
|
||||||
|
func WatchAndReload(path string) func() {
|
||||||
|
watcherMu.Lock()
|
||||||
|
defer watcherMu.Unlock()
|
||||||
|
// Check if already started
|
||||||
|
if watchStarted {
|
||||||
|
if watchStartErr != nil {
|
||||||
|
return func() {} // Previous start failed, return no-op
|
||||||
|
}
|
||||||
|
// Already running, return existing stop function
|
||||||
|
return func() {
|
||||||
|
watcherMu.Lock()
|
||||||
|
defer watcherMu.Unlock()
|
||||||
|
if watcher != nil {
|
||||||
|
if err := watcher.Close(); err != nil {
|
||||||
|
slog.Warn("failed to close config watcher", "err", err)
|
||||||
|
}
|
||||||
|
watcher = nil
|
||||||
|
watchStarted = false
|
||||||
|
slog.Info("config watcher stopped")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if path == "" {
|
if path == "" {
|
||||||
path = "config.yaml"
|
path = "config.yaml"
|
||||||
}
|
}
|
||||||
|
// Get absolute path for reliable matching
|
||||||
|
absPath, err := filepath.Abs(path)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("failed to resolve config path", "err", err)
|
||||||
|
watchStartErr = err
|
||||||
|
// Don't set watchStarted = true, allow retry
|
||||||
|
return func() {}
|
||||||
|
}
|
||||||
|
w, err := fsnotify.NewWatcher()
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("failed to create config watcher", "err", err)
|
||||||
|
watchStartErr = err
|
||||||
|
// Don't set watchStarted = true, allow retry
|
||||||
|
return func() {}
|
||||||
|
}
|
||||||
|
watchDir := filepath.Dir(absPath)
|
||||||
|
if err := w.Add(watchDir); err != nil {
|
||||||
|
slog.Error("failed to watch config directory", "err", err, "dir", watchDir)
|
||||||
|
w.Close()
|
||||||
|
watchStartErr = err
|
||||||
|
// Don't set watchStarted = true, allow retry
|
||||||
|
return func() {}
|
||||||
|
}
|
||||||
|
// Assign to global watcher before marking as started
|
||||||
|
watcher = w
|
||||||
|
watchStarted = true
|
||||||
|
watchStartErr = nil
|
||||||
|
// Create Viper instance for reading config
|
||||||
v := viper.New()
|
v := viper.New()
|
||||||
v.SetConfigFile(path)
|
v.SetConfigFile(absPath) // Use absolute path for consistency
|
||||||
v.SetConfigType("yaml")
|
v.SetConfigType("yaml")
|
||||||
|
// Set defaults
|
||||||
// Set defaults (same as Load)
|
|
||||||
def := defaults()
|
def := defaults()
|
||||||
v.SetDefault("doubao.resource_id", def.Doubao.ResourceID)
|
v.SetDefault("doubao.resource_id", def.Doubao.ResourceID)
|
||||||
v.SetDefault("server.port", def.Server.Port)
|
v.SetDefault("server.port", def.Server.Port)
|
||||||
v.SetDefault("server.tls_auto", def.Server.TLSAuto)
|
v.SetDefault("server.tls_auto", def.Server.TLSAuto)
|
||||||
|
|
||||||
v.SetEnvPrefix("voicepaste")
|
v.SetEnvPrefix("voicepaste")
|
||||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||||
v.AutomaticEnv()
|
v.AutomaticEnv()
|
||||||
|
|
||||||
// Initial read (ignore error if file doesn't exist)
|
// Initial read (ignore error if file doesn't exist)
|
||||||
if err := v.ReadInConfig(); err != nil {
|
if err := v.ReadInConfig(); err != nil {
|
||||||
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
||||||
slog.Warn("config watch: initial read failed", "err", err)
|
slog.Warn("config watch: initial read failed", "err", err)
|
||||||
|
} else {
|
||||||
|
slog.Warn("config file not found, will watch for creation", "path", path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Start event loop in goroutine
|
||||||
// Watch for changes
|
go func() {
|
||||||
v.WatchConfig()
|
for {
|
||||||
v.OnConfigChange(func(e fsnotify.Event) {
|
select {
|
||||||
slog.Info("config file changed, reloading", "file", e.Name)
|
case event, ok := <-w.Events:
|
||||||
var cfg Config
|
if !ok {
|
||||||
if err := v.Unmarshal(&cfg); err != nil {
|
return
|
||||||
slog.Error("config reload failed", "err", err)
|
}
|
||||||
return
|
// Normalize event path for comparison
|
||||||
|
eventPath := filepath.Clean(event.Name)
|
||||||
|
if eventPath != absPath {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Process Write, Create, and Rename events (common editor patterns)
|
||||||
|
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create || event.Op&fsnotify.Rename == fsnotify.Rename {
|
||||||
|
slog.Info("config file changed, reloading", "file", absPath, "op", event.Op)
|
||||||
|
// Re-read the file
|
||||||
|
if err := v.ReadInConfig(); err != nil {
|
||||||
|
slog.Error("config reload: read failed", "err", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var cfg Config
|
||||||
|
if err := v.Unmarshal(&cfg); err != nil {
|
||||||
|
slog.Error("config reload: unmarshal failed", "err", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Validate before applying
|
||||||
|
if err := validate(cfg); err != nil {
|
||||||
|
slog.Warn("config reload: validation failed, keeping old config", "err", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
store(cfg)
|
||||||
|
slog.Info("config reloaded and applied successfully")
|
||||||
|
}
|
||||||
|
case err, ok := <-w.Errors:
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
slog.Error("config watcher error", "err", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
slog.Info("config reloaded successfully")
|
}()
|
||||||
})
|
// Return stop function
|
||||||
|
return func() {
|
||||||
|
watcherMu.Lock()
|
||||||
|
defer watcherMu.Unlock()
|
||||||
|
if watcher != nil {
|
||||||
|
if err := watcher.Close(); err != nil {
|
||||||
|
slog.Warn("failed to close config watcher", "err", err)
|
||||||
|
}
|
||||||
|
watcher = nil
|
||||||
|
watchStarted = false
|
||||||
|
slog.Info("config watcher stopped")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the current config snapshot.
|
// Get returns the current config snapshot. Safe for concurrent use.
|
||||||
// Note: After switching to Viper, this is deprecated.
|
// Returns a deep copy to prevent external modifications.
|
||||||
// Use viper.Get* methods or Load() directly instead.
|
|
||||||
func Get() Config {
|
func Get() Config {
|
||||||
|
if v := global.Load(); v != nil {
|
||||||
|
cfg := v.(Config)
|
||||||
|
// Deep copy hotwords slice to prevent external modifications
|
||||||
|
if cfg.Doubao.Hotwords != nil {
|
||||||
|
cfg.Doubao.Hotwords = append([]string(nil), cfg.Doubao.Hotwords...)
|
||||||
|
}
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
return defaults()
|
return defaults()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// store updates the global config.
|
||||||
|
// Deep copies slices to ensure immutability.
|
||||||
|
func store(cfg Config) {
|
||||||
|
// Deep copy hotwords to prevent external modifications
|
||||||
|
if cfg.Doubao.Hotwords != nil {
|
||||||
|
cfg.Doubao.Hotwords = append([]string(nil), cfg.Doubao.Hotwords...)
|
||||||
|
}
|
||||||
|
global.Store(cfg)
|
||||||
|
}
|
||||||
|
|||||||
25
main.go
25
main.go
@@ -25,7 +25,12 @@ func main() {
|
|||||||
initLogger()
|
initLogger()
|
||||||
slog.Info("VoicePaste", "version", version)
|
slog.Info("VoicePaste", "version", version)
|
||||||
cfg := mustLoadConfig()
|
cfg := mustLoadConfig()
|
||||||
go config.WatchAndReload("")
|
stopWatch := config.WatchAndReload("")
|
||||||
|
defer func() {
|
||||||
|
if stopWatch != nil {
|
||||||
|
stopWatch()
|
||||||
|
}
|
||||||
|
}()
|
||||||
initClipboard()
|
initClipboard()
|
||||||
lanIPs := mustDetectLANIPs()
|
lanIPs := mustDetectLANIPs()
|
||||||
lanIP := lanIPs[0]
|
lanIP := lanIPs[0]
|
||||||
@@ -136,20 +141,22 @@ func createServer(cfg config.Config, lanIP string, tlsResult *vpTLS.Result) *ser
|
|||||||
tlsConfig = tlsResult.Config
|
tlsConfig = tlsResult.Config
|
||||||
}
|
}
|
||||||
srv := server.New(cfg.Security.Token, lanIP, webContent, tlsConfig)
|
srv := server.New(cfg.Security.Token, lanIP, webContent, tlsConfig)
|
||||||
asrFactory := buildASRFactory(cfg)
|
asrFactory := buildASRFactory()
|
||||||
wsHandler := ws.NewHandler(cfg.Security.Token, paste.Paste, asrFactory)
|
wsHandler := ws.NewHandler(cfg.Security.Token, paste.Paste, asrFactory)
|
||||||
wsHandler.Register(srv.App())
|
wsHandler.Register(srv.App())
|
||||||
return srv
|
return srv
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildASRFactory(cfg config.Config) func(chan<- ws.ServerMsg) (func([]byte), func(), error) {
|
func buildASRFactory() func(chan<- ws.ServerMsg) (func([]byte), func(), error) {
|
||||||
asrCfg := asr.Config{
|
|
||||||
AppID: cfg.Doubao.AppID,
|
|
||||||
AccessToken: cfg.Doubao.AccessToken,
|
|
||||||
ResourceID: cfg.Doubao.ResourceID,
|
|
||||||
Hotwords: cfg.Doubao.Hotwords,
|
|
||||||
}
|
|
||||||
return func(resultCh chan<- ws.ServerMsg) (func([]byte), func(), error) {
|
return func(resultCh chan<- ws.ServerMsg) (func([]byte), func(), error) {
|
||||||
|
// Read latest config on each new connection
|
||||||
|
cfg := config.Get()
|
||||||
|
asrCfg := asr.Config{
|
||||||
|
AppID: cfg.Doubao.AppID,
|
||||||
|
AccessToken: cfg.Doubao.AccessToken,
|
||||||
|
ResourceID: cfg.Doubao.ResourceID,
|
||||||
|
Hotwords: cfg.Doubao.Hotwords,
|
||||||
|
}
|
||||||
client, err := asr.Dial(asrCfg, resultCh)
|
client, err := asr.Dial(asrCfg, resultCh)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
|||||||
Reference in New Issue
Block a user