package common import ( "strings" "github.com/spf13/viper" ) type AppConfig struct { ApiPort int `mapstructure:"api-port"` SmsURL string `mapstructure:"sms-url"` Mysql Mysql `mapstructure:"mysql"` TDengine TDengine `mapstructure:"tdengine"` Redis string `mapstructure:"redis"` MQTT MQTT `mapstructure:"mqtt"` JWT JWT `mapstructure:"jwt"` Log Log `mapstructure:"log"` OSS OSS `mapstructure:"oss"` Live Live `mapstructure:"live"` Simboss Simboss `mapstructure:"simboss"` AGPay AGPay `mapstructure:"agpay"` Heartbeat Heartbeat `mapstructure:"heartbeat"` } // AppConf 全局配置(LoadConfig 后可用) var AppConf *AppConfig type Mysql struct { Url string `mapstructure:"url"` Idle int `mapstructure:"idle"` MaxConn int `mapstructure:"max-conn"` MaxWait int `mapstructure:"max-wait"` } type TDengine struct { Dsn string `mapstructure:"dsn"` } type MQTT struct { Broker string `mapstructure:"broker"` ClientId string `mapstructure:"client-id"` Username string `mapstructure:"username"` Password string `mapstructure:"password"` } type Log struct { Level string `mapstructure:"level"` Path string `mapstructure:"path"` } type JWT struct { Secret string `mapstructure:"secret"` AccessExpireH int `mapstructure:"access-expire-hours"` RefreshExpireH int `mapstructure:"refresh-expire-hours"` } // OSS 对象存储(S3 兼容,华为云 OBS / MinIO) type OSS struct { Endpoint string `mapstructure:"endpoint"` AccessKey string `mapstructure:"access-key"` SecretKey string `mapstructure:"secret-key"` Bucket string `mapstructure:"bucket"` Region string `mapstructure:"region"` UseSSL bool `mapstructure:"use-ssl"` } // Live 直播(阿里云直播) type Live struct { Mode string `mapstructure:"mode"` // disabled / fake / aliyun Provider string `mapstructure:"provider"` PushDomain string `mapstructure:"push-domain"` PlayDomain string `mapstructure:"play-domain"` AppName string `mapstructure:"app-name"` AuthKey string `mapstructure:"auth-key"` PlayAuthKey string `mapstructure:"play-auth-key"` AccessKeyID string `mapstructure:"access-key-id"` AccessKeySecret string `mapstructure:"access-key-secret"` RegionID string `mapstructure:"region-id"` APIEndpoint string `mapstructure:"api-endpoint"` AuthExpireSeconds int64 `mapstructure:"auth-expire-seconds"` PlayURLTTLSeconds int64 `mapstructure:"play-url-ttl-seconds"` Protocol string `mapstructure:"protocol"` SRTPort int `mapstructure:"srt-port"` MaxSessionSeconds int64 `mapstructure:"max-session-seconds"` ViewerLeaseSeconds int `mapstructure:"viewer-lease-seconds"` StopGraceSeconds int `mapstructure:"stop-grace-seconds"` ReconcileIntervalSeconds int `mapstructure:"reconcile-interval-seconds"` BillingIntervalSeconds int `mapstructure:"billing-interval-seconds"` RecordPolicy string `mapstructure:"record-policy"` CallbackAuthToken string `mapstructure:"callback-auth-token"` AllowRealCloud bool `mapstructure:"allow-real-cloud"` } type Simboss struct { AppID string `mapstructure:"app-id"` Secret string `mapstructure:"secret"` APIBase string `mapstructure:"api-base"` } type AGPay struct { BaseURL string `mapstructure:"base-url"` NotifyURL string `mapstructure:"notify-url"` } type Heartbeat struct { TimeoutSeconds int `mapstructure:"timeout-seconds"` ScanSeconds int `mapstructure:"scan-seconds"` } // LoadConfig 使用 Viper 加载本地 config.yaml func LoadConfig(path string, conf *AppConfig) { viper.SetConfigFile(path) viper.SetConfigType("yaml") viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_")) viper.SetEnvPrefix("LAIC") viper.AutomaticEnv() if err := viper.ReadInConfig(); err != nil { panic("加载配置文件失败: " + err.Error()) } if err := viper.Unmarshal(conf); err != nil { panic("解析配置文件失败: " + err.Error()) } AppConf = conf }