137 lines
4.2 KiB
Go
137 lines
4.2 KiB
Go
package config
|
||
|
||
import (
|
||
"flag"
|
||
"fmt"
|
||
"joylink.club/bj-rtsts-server/third_party/axle_device"
|
||
"log/slog"
|
||
"net"
|
||
"os"
|
||
"strings"
|
||
|
||
"github.com/spf13/viper"
|
||
"joylink.club/bj-rtsts-server/dto"
|
||
)
|
||
|
||
type AppConfig struct {
|
||
Env string
|
||
Server server
|
||
Datasource datasource
|
||
Logging log
|
||
Messaging messaging
|
||
Dynamics DynamicsConfig
|
||
Vobc VobcConfig
|
||
Interlock InterlockConfig
|
||
}
|
||
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 ThridPartyConfig struct {
|
||
Id int32 `json:"id"`
|
||
Dynamics DynamicsConfig `json:"dynamics" description:"动力学配置"`
|
||
Vobc VobcConfig `json:"vobc" description:"半实物配置"`
|
||
Interlocks []InterlockConfig `json:"interlock" description:"联锁配置"`
|
||
RsspAxle RsspAxleConfig `json:"rsspAxle" description:"计轴RSSP-I配置"`
|
||
}
|
||
type DynamicsConfig struct {
|
||
Ip string `json:"ip" description:"IP配置"`
|
||
UdpLocalPort int `json:"udpLocalPort" description:"本机监听接收端口"`
|
||
UdpRemotePort int `json:"udpRemotePort" description:"远端接收道岔信息端口"`
|
||
UdpRemoteTrainPort int `json:"udpRemoteTrainPort" description:"远端接收列车信息端口"`
|
||
HttpPort int `json:"httpPort" description:"http服务端口"`
|
||
Open bool `json:"open" description:"是否开启"`
|
||
}
|
||
type VobcConfig struct {
|
||
Ip string `json:"ip" description:"IP配置"`
|
||
LocalPort int `json:"localPort" description:"本机监听接收端口"`
|
||
RemotePort int `json:"remotePort" description:"远端接收列车信息端口"`
|
||
Open bool `json:"open" description:"是否开启"`
|
||
}
|
||
type InterlockConfig struct {
|
||
Ip string `json:"ip" description:"IP配置"`
|
||
LocalPort int `json:"localPort" description:"本机监听接收端口"`
|
||
RemotePort int `json:"remotePort" description:"远端接收采集信息端口"`
|
||
Open bool `json:"open" description:"是否开启"`
|
||
Code string `json:"code" description:"所属集中站"`
|
||
}
|
||
type RsspAxleConfig struct {
|
||
MasterRssp axle_device.RsspConfig `json:"masterRssp" description:"主安全通道配置"`
|
||
SlaveRssp axle_device.RsspConfig `json:"slaveRssp" description:"备安全通道配置"`
|
||
}
|
||
|
||
var Config AppConfig
|
||
|
||
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]
|
||
})()
|
||
|
||
// 获取配置文件名称,从运行flag参数config中获取,若未提供,使用默认'dev'
|
||
func getConfigName() string {
|
||
configName := ""
|
||
flag.StringVar(&configName, "config", "dev", "config name, eg: -config test")
|
||
flag.Parse()
|
||
if configName == "" {
|
||
configName = "dev"
|
||
}
|
||
slog.Info("读取配置文件", "配置文件名称", 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))
|
||
}
|
||
slog.Info("成功加载配置", "config", Config)
|
||
}
|