Files
EchoChat/backend/go-service/config/config.go
bujinyuan 55d6352c6a feat(phase2e-2): 落地 HTTPMediaOrchestrator 打通 Go↔Node 媒体链路(Task 7)
- 新增 MediaServerConfig + config.{dev,docker}.yaml 的 media_server 段
  (base_url / internal_token / timeout_ms / close_timeout_ms / close_retry)
- 新建 HTTPMediaOrchestrator(8 方法 + sync.Map 缓存 roomCode→routerID
  + 关闭类指数退避重试 200/500ms + ErrMediaResourceNotFound/ErrMediaServerError
  两类错误)实现 MediaOrchestrator 接口
- wire 绑定由 NoopMediaOrchestrator 切换到 HTTPMediaOrchestrator;
  MeetingService / MeetingSignalService 调用侧零改动
- E2E 脚本 docs/verify/meeting_t7_verify.mjs 证明 16/16 PASS:
  健康检查/错token 401/REST 创房加入/WS room.join/真实 mediasoup
  transport.create(ICE/DTLS 指纹均由 Node 返回非占位)/404 幂等关 producer
  /host 结束会议触发 CloseRouter
- 同步更新 13 份文档:CURRENT_STATUS / implementation.plan / design
  变更记录 / project-context / api 导览 / api websocket / api frontend meeting
  / system-architecture / media-server README / 顶层 README 等
- go build ./... 全绿

Made-with: Cursor
2026-04-21 17:47:11 +08:00

132 lines
5.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package config 提供应用配置管理功能
// 使用 Viper 读取 YAML 配置文件,支持环境变量覆盖
package config
import (
"fmt"
"strings"
"github.com/spf13/viper"
)
// Config 应用全局配置结构体
type Config struct {
Server ServerConfig `mapstructure:"server"`
Database DatabaseConfig `mapstructure:"database"`
Redis RedisConfig `mapstructure:"redis"`
JWT JWTConfig `mapstructure:"jwt"`
Log LogConfig `mapstructure:"log"`
Minio MinioConfig `mapstructure:"minio"`
MediaServer MediaServerConfig `mapstructure:"media_server"`
}
// MediaServerConfig Node media-server 接入配置Phase 2e-2 Task 7
// 与 media-server/.env 中的 MEDIA_INTERNAL_TOKEN / HTTP_PORT 成对使用
// BaseURL 需精确到协议与端口http://host:port不含末尾斜杠
type MediaServerConfig struct {
BaseURL string `mapstructure:"base_url"` // 如 http://localhost:3300
InternalToken string `mapstructure:"internal_token"` // 与 Node 共享密钥
TimeoutMS int `mapstructure:"timeout_ms"` // 创建类接口超时(毫秒),默认 5000
CloseTimeoutMS int `mapstructure:"close_timeout_ms"` // 关闭类接口超时(毫秒),默认 2000
CloseRetry int `mapstructure:"close_retry"` // 关闭类接口失败重试次数,默认 2
}
// MinioConfig MinIO 对象存储配置
type MinioConfig struct {
Endpoint string `mapstructure:"endpoint"` // MinIO 服务地址host:port
AccessKey string `mapstructure:"access_key"` // 访问密钥
SecretKey string `mapstructure:"secret_key"` // 密钥
Bucket string `mapstructure:"bucket"` // 存储桶名称
UseSSL bool `mapstructure:"use_ssl"` // 是否使用 HTTPS
}
// ServerConfig HTTP 服务配置
type ServerConfig struct {
Port int `mapstructure:"port"` // 监听端口
Mode string `mapstructure:"mode"` // 运行模式: debug/release
}
// DatabaseConfig PostgreSQL 数据库配置
type DatabaseConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
DBName string `mapstructure:"dbname"`
SSLMode string `mapstructure:"sslmode"`
MaxIdleConns int `mapstructure:"max_idle_conns"` // 最大空闲连接数
MaxOpenConns int `mapstructure:"max_open_conns"` // 最大打开连接数
}
// DSN 生成 PostgreSQL 连接字符串
func (d *DatabaseConfig) DSN() string {
return fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
d.Host, d.Port, d.User, d.Password, d.DBName, d.SSLMode,
)
}
// RedisConfig Redis 配置
type RedisConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Password string `mapstructure:"password"`
DB int `mapstructure:"db"` // 数据库编号
}
// Addr 生成 Redis 连接地址
func (r *RedisConfig) Addr() string {
return fmt.Sprintf("%s:%d", r.Host, r.Port)
}
// JWTConfig JWT 认证配置
type JWTConfig struct {
Secret string `mapstructure:"secret"` // 签名密钥
AccessExpireMin int `mapstructure:"access_expire_min"` // Access Token 有效期(分钟)
RefreshExpireDay int `mapstructure:"refresh_expire_day"` // Refresh Token 有效期(天)
Issuer string `mapstructure:"issuer"` // 签发者
}
// LogConfig 日志配置
type LogConfig struct {
Level string `mapstructure:"level"` // debug/info/warn/error
Format string `mapstructure:"format"` // text(开发)/json(生产)
OutputPath string `mapstructure:"output_path"` // stdout 或文件路径
File LogFileConfig `mapstructure:"file"` // 日志文件轮转配置
}
// LogFileConfig 日志文件轮转配置(基于 lumberjack
type LogFileConfig struct {
Enable bool `mapstructure:"enable"` // 是否启用文件日志
Dir string `mapstructure:"dir"` // 日志文件目录
MaxSize int `mapstructure:"max_size"` // 单个文件最大大小MB超过后自动切割
MaxBackups int `mapstructure:"max_backups"` // 保留的旧日志文件最大数量
MaxAge int `mapstructure:"max_age"` // 旧日志文件保留天数
Compress bool `mapstructure:"compress"` // 是否压缩归档的旧日志文件
}
// Load 加载配置文件并返回 Config 实例
// configPath 为配置文件所在目录configName 为文件名(不含扩展名)
func Load(configPath, configName string) (*Config, error) {
v := viper.New()
v.SetConfigName(configName)
v.SetConfigType("yaml")
v.AddConfigPath(configPath)
// 环境变量覆盖ECHOCHAT_SERVER_PORT → server.port
v.SetEnvPrefix("ECHOCHAT")
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.AutomaticEnv()
if err := v.ReadInConfig(); err != nil {
return nil, fmt.Errorf("读取配置文件失败: %w", err)
}
var cfg Config
if err := v.Unmarshal(&cfg); err != nil {
return nil, fmt.Errorf("解析配置文件失败: %w", err)
}
return &cfg, nil
}