refactor: 重构配置结构,解耦热词、统一认证、移除 TLS 开关

- 新增 ASRConfig,热词从 doubao 提升为 provider 无关配置
- 移除 SecurityConfig,token 移入 ServerConfig
- 移除 tls_auto 配置项,TLS 始终启用(getUserMedia 要求 HTTPS)
- validate() 改为基于 provider 白名单验证,增加 resource_id 校验
- 简化 main.go:移除 scheme 变量和 HTTP 降级分支
- 更新 config.example.yaml 为新结构并修正环境变量前缀
This commit is contained in:
2026-03-02 04:36:22 +08:00
parent 0720505ef6
commit 48c8444b3f
3 changed files with 81 additions and 70 deletions

View File

@@ -7,45 +7,48 @@ import (
"strings"
"sync"
"sync/atomic"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
// DoubaoConfig holds 火山引擎豆包 ASR credentials.
type DoubaoConfig struct {
AppID string `mapstructure:"app_id"`
AccessToken string `mapstructure:"access_token"`
ResourceID string `mapstructure:"resource_id"`
Hotwords []string `mapstructure:"hotwords"` // 本地热词列表
// ASRConfig holds ASR settings independent of any specific provider.
type ASRConfig struct {
Provider string `mapstructure:"provider"`
Hotwords []string `mapstructure:"hotwords"` // 通用热词列表
}
// SecurityConfig holds authentication settings.
type SecurityConfig struct {
Token string `mapstructure:"token"`
// DoubaoConfig holds 火山引擎豆包 ASR credentials.
type DoubaoConfig struct {
AppID string `mapstructure:"app_id"`
AccessToken string `mapstructure:"access_token"`
ResourceID string `mapstructure:"resource_id"`
}
// ServerConfig holds server settings.
type ServerConfig struct {
Port int `mapstructure:"port"`
TLSAuto bool `mapstructure:"tls_auto"`
Port int `mapstructure:"port"`
Token string `mapstructure:"token"`
}
// Config is the top-level configuration.
type Config struct {
Doubao DoubaoConfig `mapstructure:"doubao"`
Server ServerConfig `mapstructure:"server"`
Security SecurityConfig `mapstructure:"security"`
ASR ASRConfig `mapstructure:"asr"`
Doubao DoubaoConfig `mapstructure:"doubao"`
Server ServerConfig `mapstructure:"server"`
}
// defaults returns a Config with default values.
func defaults() Config {
return Config{
ASR: ASRConfig{
Provider: "doubao",
},
Doubao: DoubaoConfig{
ResourceID: "volc.seedasr.sauc.duration",
},
Server: ServerConfig{
Port: 8443,
TLSAuto: true,
Port: 8443,
},
}
}
@@ -70,9 +73,9 @@ func Load(path string) (Config, error) {
// Set defaults
def := defaults()
v.SetDefault("asr.provider", def.ASR.Provider)
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")
@@ -98,13 +101,23 @@ func Load(path string) (Config, error) {
store(cfg)
return cfg, nil
}
// validate checks required fields.
// validate checks required fields based on the configured ASR provider.
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")
provider := strings.TrimSpace(strings.ToLower(cfg.ASR.Provider))
switch provider {
case "doubao":
if cfg.Doubao.AppID == "" {
return fmt.Errorf("doubao.app_id is required when asr.provider is \"doubao\"")
}
if cfg.Doubao.AccessToken == "" {
return fmt.Errorf("doubao.access_token is required when asr.provider is \"doubao\"")
}
if cfg.Doubao.ResourceID == "" {
return fmt.Errorf("doubao.resource_id is required when asr.provider is \"doubao\"")
}
default:
return fmt.Errorf("unsupported asr.provider: %q (supported: doubao)", cfg.ASR.Provider)
}
return nil
}
@@ -170,9 +183,9 @@ func WatchAndReload(path string) func() {
v.SetConfigType("yaml")
// Set defaults
def := defaults()
v.SetDefault("asr.provider", def.ASR.Provider)
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()
@@ -247,8 +260,8 @@ 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...)
if cfg.ASR.Hotwords != nil {
cfg.ASR.Hotwords = append([]string(nil), cfg.ASR.Hotwords...)
}
return cfg
}
@@ -259,8 +272,8 @@ func Get() 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...)
if cfg.ASR.Hotwords != nil {
cfg.ASR.Hotwords = append([]string(nil), cfg.ASR.Hotwords...)
}
global.Store(cfg)
}