refactor: 使用 Viper 替换手动配置管理,支持原生热重载
This commit is contained in:
@@ -1,33 +1,37 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// DoubaoConfig holds 火山引擎豆包 ASR credentials.
|
||||
type DoubaoConfig struct {
|
||||
AppID string `yaml:"app_id"`
|
||||
AccessToken string `yaml:"access_token"`
|
||||
ResourceID string `yaml:"resource_id"`
|
||||
Hotwords []string `yaml:"hotwords"` // 本地热词列表
|
||||
AppID string `mapstructure:"app_id"`
|
||||
AccessToken string `mapstructure:"access_token"`
|
||||
ResourceID string `mapstructure:"resource_id"`
|
||||
Hotwords []string `mapstructure:"hotwords"` // 本地热词列表
|
||||
}
|
||||
|
||||
// SecurityConfig holds authentication settings.
|
||||
type SecurityConfig struct {
|
||||
Token string `yaml:"token"`
|
||||
Token string `mapstructure:"token"`
|
||||
}
|
||||
|
||||
// ServerConfig holds server settings.
|
||||
type ServerConfig struct {
|
||||
Port int `yaml:"port"`
|
||||
TLSAuto bool `yaml:"tls_auto"`
|
||||
Port int `mapstructure:"port"`
|
||||
TLSAuto bool `mapstructure:"tls_auto"`
|
||||
}
|
||||
|
||||
// Config is the top-level configuration.
|
||||
type Config struct {
|
||||
Doubao DoubaoConfig `yaml:"doubao"`
|
||||
Server ServerConfig `yaml:"server"`
|
||||
Security SecurityConfig `yaml:"security"`
|
||||
Doubao DoubaoConfig `mapstructure:"doubao"`
|
||||
Server ServerConfig `mapstructure:"server"`
|
||||
Security SecurityConfig `mapstructure:"security"`
|
||||
}
|
||||
|
||||
// defaults returns a Config with default values.
|
||||
@@ -43,18 +47,88 @@ func defaults() Config {
|
||||
}
|
||||
}
|
||||
|
||||
// global holds the current config atomically for concurrent reads.
|
||||
var global atomic.Value
|
||||
|
||||
// Get returns the current config snapshot. Safe for concurrent use.
|
||||
func Get() Config {
|
||||
if v := global.Load(); v != nil {
|
||||
return v.(Config)
|
||||
// Load reads config from file (or uses defaults if file doesn't exist).
|
||||
// Empty path defaults to "config.yaml".
|
||||
func Load(path string) (Config, error) {
|
||||
if path == "" {
|
||||
path = "config.yaml"
|
||||
}
|
||||
|
||||
v := viper.New()
|
||||
v.SetConfigFile(path)
|
||||
v.SetConfigType("yaml")
|
||||
|
||||
// Set defaults
|
||||
def := defaults()
|
||||
v.SetDefault("doubao.resource_id", def.Doubao.ResourceID)
|
||||
v.SetDefault("server.port", def.Server.Port)
|
||||
v.SetDefault("server.tls_auto", def.Server.TLSAuto)
|
||||
|
||||
// Allow env var overrides (e.g., VOICEPASTE_DOUBAO_APP_ID)
|
||||
v.SetEnvPrefix("voicepaste")
|
||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
v.AutomaticEnv()
|
||||
|
||||
// Read config file (ignore error if file doesn't exist)
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
||||
return Config{}, err
|
||||
}
|
||||
slog.Warn("config file not found, using defaults", "path", path)
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := v.Unmarshal(&cfg); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// WatchAndReload starts watching config file for changes and reloads automatically.
|
||||
// Empty path defaults to "config.yaml".
|
||||
func WatchAndReload(path string) {
|
||||
if path == "" {
|
||||
path = "config.yaml"
|
||||
}
|
||||
|
||||
v := viper.New()
|
||||
v.SetConfigFile(path)
|
||||
v.SetConfigType("yaml")
|
||||
|
||||
// Set defaults (same as Load)
|
||||
def := defaults()
|
||||
v.SetDefault("doubao.resource_id", def.Doubao.ResourceID)
|
||||
v.SetDefault("server.port", def.Server.Port)
|
||||
v.SetDefault("server.tls_auto", def.Server.TLSAuto)
|
||||
|
||||
v.SetEnvPrefix("voicepaste")
|
||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
v.AutomaticEnv()
|
||||
|
||||
// Initial read (ignore error if file doesn't exist)
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
||||
slog.Warn("config watch: initial read failed", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Watch for changes
|
||||
v.WatchConfig()
|
||||
v.OnConfigChange(func(e fsnotify.Event) {
|
||||
slog.Info("config file changed, reloading", "file", e.Name)
|
||||
var cfg Config
|
||||
if err := v.Unmarshal(&cfg); err != nil {
|
||||
slog.Error("config reload failed", "err", err)
|
||||
return
|
||||
}
|
||||
slog.Info("config reloaded successfully")
|
||||
})
|
||||
}
|
||||
|
||||
// Get returns the current config snapshot.
|
||||
// Note: After switching to Viper, this is deprecated.
|
||||
// Use viper.Get* methods or Load() directly instead.
|
||||
func Get() Config {
|
||||
return defaults()
|
||||
}
|
||||
|
||||
// store updates the global config.
|
||||
func store(cfg Config) {
|
||||
global.Store(cfg)
|
||||
}
|
||||
|
||||
@@ -1,126 +0,0 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// Load reads config from file (optional), applies env overrides, validates, and stores globally.
|
||||
// If configPath is empty, it tries "config.yaml" in the working directory.
|
||||
func Load(configPath string) (Config, error) {
|
||||
cfg := defaults()
|
||||
|
||||
// Try loading YAML file
|
||||
if configPath == "" {
|
||||
configPath = "config.yaml"
|
||||
}
|
||||
if data, err := os.ReadFile(configPath); err == nil {
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
return cfg, fmt.Errorf("parse config %s: %w", configPath, err)
|
||||
}
|
||||
slog.Info("loaded config file", "path", configPath)
|
||||
}
|
||||
|
||||
// Env overrides
|
||||
applyEnv(&cfg)
|
||||
|
||||
// Validate
|
||||
if err := validate(cfg); err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
store(cfg)
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// applyEnv overrides config fields with environment variables.
|
||||
func applyEnv(cfg *Config) {
|
||||
envStringMap := map[string]*string{
|
||||
"DOUBAO_APP_ID": &cfg.Doubao.AppID,
|
||||
"DOUBAO_ACCESS_TOKEN": &cfg.Doubao.AccessToken,
|
||||
"DOUBAO_RESOURCE_ID": &cfg.Doubao.ResourceID,
|
||||
}
|
||||
for key, target := range envStringMap {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
*target = v
|
||||
}
|
||||
}
|
||||
if v := os.Getenv("PORT"); v != "" {
|
||||
if port, err := strconv.Atoi(v); err == nil {
|
||||
cfg.Server.Port = port
|
||||
}
|
||||
}
|
||||
if v := os.Getenv("TLS_AUTO"); v != "" {
|
||||
cfg.Server.TLSAuto = v == "true" || v == "1"
|
||||
}
|
||||
}
|
||||
|
||||
// validate checks required fields.
|
||||
func validate(cfg Config) error {
|
||||
if cfg.Doubao.AppID == "" {
|
||||
return fmt.Errorf("doubao.app_id is required (set DOUBAO_APP_ID or config.yaml)")
|
||||
}
|
||||
if cfg.Doubao.AccessToken == "" {
|
||||
return fmt.Errorf("doubao.access_token is required (set DOUBAO_ACCESS_TOKEN or config.yaml)")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WatchAndReload watches the config file for changes and hot-reloads.
|
||||
func WatchAndReload(configPath string) {
|
||||
if configPath == "" {
|
||||
configPath = "config.yaml"
|
||||
}
|
||||
|
||||
absPath, err := filepath.Abs(configPath)
|
||||
if err != nil {
|
||||
slog.Warn("cannot resolve config path for watching", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
slog.Warn("cannot create file watcher", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
dir := filepath.Dir(absPath)
|
||||
if err := watcher.Add(dir); err != nil {
|
||||
slog.Warn("cannot watch config directory", "error", err)
|
||||
watcher.Close()
|
||||
return
|
||||
}
|
||||
|
||||
slog.Info("watching config for changes", "path", absPath)
|
||||
|
||||
go func() {
|
||||
defer watcher.Close()
|
||||
for {
|
||||
select {
|
||||
case event, ok := <-watcher.Events:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if filepath.Clean(event.Name) == absPath && (event.Has(fsnotify.Write) || event.Has(fsnotify.Create)) {
|
||||
slog.Info("config file changed, reloading")
|
||||
if _, err := Load(configPath); err != nil {
|
||||
slog.Error("failed to reload config", "error", err)
|
||||
} else {
|
||||
slog.Info("config reloaded successfully")
|
||||
}
|
||||
}
|
||||
case err, ok := <-watcher.Errors:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
slog.Error("config watcher error", "error", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user