From 2513d4a747c78caca9af89d3f62044ebe755171a Mon Sep 17 00:00:00 2001 From: walker Date: Fri, 18 Aug 2023 10:22:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=91=BD=E4=BB=A4=E8=A1=8C?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E6=8C=87=E5=AE=9A=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=90=8D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 +- config/config.go | 26 ++++++++++++++++------ config.yml => config/dev.yml | 8 +++---- config/test_local.yml | 42 ++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 11 deletions(-) rename config.yml => config/dev.yml (91%) create mode 100644 config/test_local.yml diff --git a/Dockerfile b/Dockerfile index 4fcd270..de378ad 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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" ] \ No newline at end of file diff --git a/config/config.go b/config/config.go index 02a90b4..4f8946c 100644 --- a/config/config.go +++ b/config/config.go @@ -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)) } diff --git a/config.yml b/config/dev.yml similarity index 91% rename from config.yml rename to config/dev.yml index 54c6129..37c5c72 100644 --- a/config.yml +++ b/config/dev.yml @@ -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 # 控制台是否输出 diff --git a/config/test_local.yml b/config/test_local.yml new file mode 100644 index 0000000..61bfe72 --- /dev/null +++ b/config/test_local.yml @@ -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