rts-sim-testing-service/config/config.go

85 lines
1.9 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
import (
"flag"
"fmt"
"os"
"github.com/spf13/viper"
)
type AppConfig struct {
Env string
Server server
Datasource datasource
Logging log
Messaging messaging
Dynamics dynamics
}
type server struct {
Port int
}
type datasource struct {
Dsn string
}
type log struct {
Level string // 日志打印级别 debug info warn error
Format string // 输出日志格式 logfmt, json
Path string // 输出日志文件路径
FileName string // 输出日志文件名称
FileMaxSize int // 【日志分割】单个日志文件最多存储量 单位(mb)
FileMaxBackups int // 【日志分割】日志备份文件最多数量
MaxAge int // 日志保留时间,单位: 天 (day)
Compress bool // 是否压缩日志
Stdout bool // 是否输出到控制台
}
type messaging struct {
Centrifugo centrifugo
}
type centrifugo struct {
TokenSecret string
ApiKey string
ApiEndpoint string
Address string
}
type dynamics struct {
Ip string
UdpLocalPort int
UdpRemotePort int
HttpPort int
}
var Config AppConfig
// 获取配置文件名称,从运行flag参数config中获取若未提供使用默认'dev'
func getConfigName() string {
configName := ""
flag.StringVar(&configName, "config", "dev", "config name, eg: -config test")
flag.Parse()
if configName == "" {
configName = "dev"
}
fmt.Println("config name:", configName)
return configName
}
// 加载配置
func LoadConfig() {
cnf := viper.New()
cnf.SetConfigName(getConfigName())
cnf.SetConfigType("yml")
cnf.AddConfigPath("./config/")
cnf.AddConfigPath(".")
err := cnf.ReadInConfig()
if err != nil {
panic(fmt.Errorf("读取配置文件错误: %w", err))
}
fmt.Println(os.Args)
err = cnf.Unmarshal(&Config)
if err != nil {
panic(fmt.Errorf("解析配置文件错误: %w", err))
}
fmt.Println(Config)
}