2023-07-14 10:16:58 +08:00
|
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
2023-08-18 10:22:17 +08:00
|
|
|
|
"flag"
|
2023-07-14 10:16:58 +08:00
|
|
|
|
"fmt"
|
2023-10-12 10:10:23 +08:00
|
|
|
|
"log/slog"
|
2023-08-31 14:06:53 +08:00
|
|
|
|
"net"
|
2023-07-14 10:16:58 +08:00
|
|
|
|
"os"
|
2023-08-31 14:06:53 +08:00
|
|
|
|
"strings"
|
2023-07-14 10:16:58 +08:00
|
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
2023-08-31 14:06:53 +08:00
|
|
|
|
"joylink.club/bj-rtsts-server/dto"
|
2023-07-14 10:16:58 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type AppConfig struct {
|
|
|
|
|
Env string
|
|
|
|
|
Server server
|
|
|
|
|
Datasource datasource
|
|
|
|
|
Logging log
|
2023-07-26 17:51:32 +08:00
|
|
|
|
Messaging messaging
|
2023-10-26 15:06:26 +08:00
|
|
|
|
Dynamics DynamicsConfig
|
|
|
|
|
Vobc VobcConfig
|
|
|
|
|
Interlock InterlockConfig
|
2023-07-14 10:16:58 +08:00
|
|
|
|
}
|
|
|
|
|
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 // 是否输出到控制台
|
|
|
|
|
}
|
2023-07-26 18:00:00 +08:00
|
|
|
|
|
2023-07-26 17:51:32 +08:00
|
|
|
|
type messaging struct {
|
|
|
|
|
Centrifugo centrifugo
|
|
|
|
|
}
|
|
|
|
|
type centrifugo struct {
|
|
|
|
|
TokenSecret string
|
|
|
|
|
ApiKey string
|
|
|
|
|
ApiEndpoint string
|
|
|
|
|
Address string
|
2023-07-26 18:00:00 +08:00
|
|
|
|
}
|
2023-10-26 15:06:26 +08:00
|
|
|
|
|
|
|
|
|
// 第三方配置结构
|
|
|
|
|
type ThridPartyConfig struct {
|
|
|
|
|
Dynamics DynamicsConfig `json:"dynamics"`
|
|
|
|
|
Vobc VobcConfig `json:"vobc"`
|
|
|
|
|
Interlock InterlockConfig `json:"interlock"`
|
2023-07-26 17:02:53 +08:00
|
|
|
|
}
|
2023-10-26 15:06:26 +08:00
|
|
|
|
type DynamicsConfig struct {
|
|
|
|
|
Ip string `json:"ip"`
|
|
|
|
|
UdpLocalPort int `json:"udpLocalPort"`
|
|
|
|
|
UdpRemotePort int `json:"udpRemotePort"`
|
|
|
|
|
UdpRemoteTrainPort int `json:"udpRemoteTrainPort"`
|
|
|
|
|
HttpPort int `json:"httpPort"`
|
|
|
|
|
Open bool `json:"open"`
|
2023-08-18 16:20:40 +08:00
|
|
|
|
}
|
2023-10-26 15:06:26 +08:00
|
|
|
|
type VobcConfig struct {
|
|
|
|
|
Ip string `json:"ip"`
|
|
|
|
|
LocalPort int `json:"localPort"`
|
|
|
|
|
RemotePort int `json:"remotePort"`
|
|
|
|
|
Open bool `json:"open"`
|
|
|
|
|
}
|
|
|
|
|
type InterlockConfig struct {
|
|
|
|
|
Ip string `json:"ip"`
|
|
|
|
|
LocalPort int `json:"localPort"`
|
|
|
|
|
RemotePort int `json:"remotePort"`
|
|
|
|
|
Open bool `json:"open"`
|
2023-10-24 10:57:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-14 10:16:58 +08:00
|
|
|
|
var Config AppConfig
|
|
|
|
|
|
2023-08-31 14:06:53 +08:00
|
|
|
|
var SimulationId_prefix = (func() string {
|
|
|
|
|
ip := "127.0.0.1"
|
|
|
|
|
addrList, err := net.InterfaceAddrs()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
|
|
|
|
|
}
|
|
|
|
|
for _, address := range addrList {
|
|
|
|
|
if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
|
|
|
|
|
if ipNet.IP.To4() != nil {
|
|
|
|
|
ip = ipNet.IP.String()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ipArr := strings.Split(ip, ".")
|
|
|
|
|
return ipArr[2] + "_" + ipArr[3]
|
|
|
|
|
})()
|
|
|
|
|
|
2023-08-18 10:22:17 +08:00
|
|
|
|
// 获取配置文件名称,从运行flag参数config中获取,若未提供,使用默认'dev'
|
|
|
|
|
func getConfigName() string {
|
|
|
|
|
configName := ""
|
|
|
|
|
flag.StringVar(&configName, "config", "dev", "config name, eg: -config test")
|
|
|
|
|
flag.Parse()
|
|
|
|
|
if configName == "" {
|
|
|
|
|
configName = "dev"
|
|
|
|
|
}
|
2023-10-12 10:10:23 +08:00
|
|
|
|
slog.Info("读取配置文件", "配置文件名称", configName)
|
2023-08-18 10:22:17 +08:00
|
|
|
|
return configName
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-14 10:16:58 +08:00
|
|
|
|
// 加载配置
|
|
|
|
|
func LoadConfig() {
|
2023-08-18 10:22:17 +08:00
|
|
|
|
cnf := viper.New()
|
|
|
|
|
cnf.SetConfigName(getConfigName())
|
|
|
|
|
cnf.SetConfigType("yml")
|
|
|
|
|
cnf.AddConfigPath("./config/")
|
|
|
|
|
cnf.AddConfigPath(".")
|
|
|
|
|
err := cnf.ReadInConfig()
|
2023-07-14 10:16:58 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(fmt.Errorf("读取配置文件错误: %w", err))
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(os.Args)
|
2023-08-18 10:22:17 +08:00
|
|
|
|
err = cnf.Unmarshal(&Config)
|
2023-07-14 10:16:58 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(fmt.Errorf("解析配置文件错误: %w", err))
|
|
|
|
|
}
|
2023-10-12 10:10:23 +08:00
|
|
|
|
slog.Info("成功加载配置", "config", Config)
|
2023-07-14 10:16:58 +08:00
|
|
|
|
}
|