You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.4 KiB
105 lines
3.4 KiB
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"`
|
|
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"`
|
|
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"`
|
|
RecordPolicy string `mapstructure:"record-policy"`
|
|
AllowRealCloud bool `mapstructure:"allow-real-cloud"`
|
|
}
|
|
|
|
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
|
|
}
|
|
|