feat: add config loading with YAML, env override, and hot-reload

This commit is contained in:
2026-03-01 03:02:57 +08:00
parent 97ecd175a7
commit 6b74ad399d
2 changed files with 178 additions and 0 deletions

53
internal/config/config.go Normal file
View File

@@ -0,0 +1,53 @@
package config
import (
"sync/atomic"
)
// DoubaoConfig holds 火山引擎豆包 ASR credentials.
type DoubaoConfig struct {
AppKey string `yaml:"app_key"`
AccessKey string `yaml:"access_key"`
ResourceID string `yaml:"resource_id"`
}
// ServerConfig holds server settings.
type ServerConfig struct {
Port int `yaml:"port"`
TLSAuto bool `yaml:"tls_auto"`
}
// Config is the top-level configuration.
type Config struct {
Doubao DoubaoConfig `yaml:"doubao"`
Server ServerConfig `yaml:"server"`
}
// defaults returns a Config with default values.
func defaults() Config {
return Config{
Doubao: DoubaoConfig{
ResourceID: "volc.seedasr.sauc.duration",
},
Server: ServerConfig{
Port: 8443,
TLSAuto: true,
},
}
}
// 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)
}
return defaults()
}
// store updates the global config.
func store(cfg Config) {
global.Store(cfg)
}