添加命令行可以指定配置文件名功能
This commit is contained in:
parent
cd31225af2
commit
2513d4a747
|
@ -6,5 +6,5 @@ RUN go env -w GO111MODULE=on && go env -w GOPROXY=https://goproxy.cn,direct && g
|
|||
FROM scratch
|
||||
COPY --from=builder /go/src/joylink.club/bj-rtsts-server/bj-rtsts-server /usr/local/bin/bj-rtsts-server
|
||||
WORKDIR /bj-rtsts
|
||||
COPY --from=builder /go/src/joylink.club/bj-rtsts-server/config.yml .
|
||||
COPY --from=builder /go/src/joylink.club/bj-rtsts-server/config/*.yml .
|
||||
CMD [ "bj-rtsts-server" ]
|
|
@ -1,6 +1,7 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
|
@ -51,18 +52,31 @@ type dynamics struct {
|
|||
|
||||
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() {
|
||||
viper.SetConfigName("config")
|
||||
viper.SetConfigType("yml")
|
||||
viper.AddConfigPath("./config/")
|
||||
viper.AddConfigPath(".")
|
||||
err := viper.ReadInConfig()
|
||||
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 = viper.Unmarshal(&Config)
|
||||
err = cnf.Unmarshal(&Config)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("解析配置文件错误: %w", err))
|
||||
}
|
||||
|
|
|
@ -21,15 +21,15 @@ logging:
|
|||
# 格式化
|
||||
# format: json
|
||||
# 日志文件路径
|
||||
path: /logs/xiannccda
|
||||
path: /logs/bjrtsts
|
||||
# 日志文件名
|
||||
fileName: test.log
|
||||
fileName: bjrtsts.log
|
||||
# 单个日志文件大小,单位mb,超过会自动滚动
|
||||
fileMaxSize: 2
|
||||
fileMaxSize: 5
|
||||
# 日志文件最大备份数量
|
||||
fileMaxBackups: 100
|
||||
# 日志文件最大保留时间,单位 天
|
||||
maxAge: 10
|
||||
maxAge: 30
|
||||
# 是否压缩日志
|
||||
compress: false
|
||||
# 控制台是否输出
|
|
@ -0,0 +1,42 @@
|
|||
# dev-开发环境 test-测试环境 prod-生产环境
|
||||
env: test
|
||||
# 服务配置
|
||||
server:
|
||||
# 服务端口
|
||||
port: 9091
|
||||
# 动力学端口配置
|
||||
dynamics:
|
||||
ip: 192.168.3.94
|
||||
udpLocalPort: 4000
|
||||
udpRemotePort: 3000
|
||||
httpPort: 7800
|
||||
|
||||
# 数据源
|
||||
datasource:
|
||||
# 数据库访问url
|
||||
dsn: root:joylink0503@tcp(192.168.53.22:3306)/bj-rtss?charset=utf8mb4&parseTime=true&loc=Local
|
||||
logging:
|
||||
# 日志级别
|
||||
level: info
|
||||
# 格式化
|
||||
# format: json
|
||||
# 日志文件路径
|
||||
path: /logs/bjrtsts
|
||||
# 日志文件名
|
||||
fileName: bjrtsts.log
|
||||
# 单个日志文件大小,单位mb,超过会自动滚动
|
||||
fileMaxSize: 5
|
||||
# 日志文件最大备份数量
|
||||
fileMaxBackups: 100
|
||||
# 日志文件最大保留时间,单位 天
|
||||
maxAge: 30
|
||||
# 是否压缩日志
|
||||
compress: false
|
||||
# 控制台是否输出
|
||||
stdout: false
|
||||
messaging:
|
||||
centrifugo:
|
||||
tokenSecret: aaa
|
||||
apiKey: bbb
|
||||
apiEndpoint: /api
|
||||
address: 192.168.3.233:10000
|
Loading…
Reference in New Issue