From bfda990caefc8ad4f536bb0d60d981b46019748c Mon Sep 17 00:00:00 2001 From: SliverHorn <503551462@qq.com> Date: Wed, 23 Sep 2020 19:04:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20=E4=BC=98=E5=85=88?= =?UTF-8?q?=E7=BA=A7:=20=E5=91=BD=E4=BB=A4=E8=A1=8C=20>=20=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=20>=20=E9=BB=98=E8=AE=A4=E5=80=BC?= =?UTF-8?q?=20=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/config.yaml | 1 + server/config/config.go | 3 ++- server/core/config.go | 33 ++++++++++++++++++--------------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/server/config.yaml b/server/config.yaml index 4f01e1a1..c754c3d8 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -78,6 +78,7 @@ system: db-type: "mysql" # support mysql/postgresql/sqlite/sqlserver need-init-data: false error-to-email: false + config-env: "GVA_CONFIG" # captcha configuration captcha: diff --git a/server/config/config.go b/server/config/config.go index 6d75ca78..43bae8bb 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -22,7 +22,8 @@ type System struct { Addr int `mapstructure:"addr" json:"addr" yaml:"addr"` DbType string `mapstructure:"db-type" json:"dbType" yaml:"db-type"` NeedInitData bool `mapstructure:"need-init-data" json:"needInitData" yaml:"need-init-data"` - ErrorToEmail bool `mapstructure:"error-to-email" json:"errorToEmail" yaml:"error-to-email"` + ErrorToEmail bool `mapstructure:"error-to-email" json:"errorToEmail" yaml:"error-to-email"` + ConfigEnv string `mapstructure:"config-env" json:"configEnv" yaml:"config-env"` } type JWT struct { diff --git a/server/core/config.go b/server/core/config.go index c06bcca1..e3ac9094 100644 --- a/server/core/config.go +++ b/server/core/config.go @@ -1,32 +1,35 @@ package core import ( + "flag" "fmt" "gin-vue-admin/global" _ "gin-vue-admin/packfile" "github.com/fsnotify/fsnotify" "github.com/spf13/viper" - "github.com/spf13/pflag" + "os" ) +var config string + const defaultConfigFile = "config.yaml" func init() { - pflag.StringP("configFile","c", "", "choose config file.") - pflag.Parse() - - // 优先级: 命令行 > 环境变量 > 默认值 - v := viper.New() - v.BindPFlags(pflag.CommandLine) - v.SetEnvPrefix("gva") - v.BindEnv("configFile") // GVA_CONFIGFILE - - configFile := v.GetString("configFile") - if configFile == ""{ - configFile = defaultConfigFile + flag.StringVar(&config, "c", "", "choose config file.") + flag.Parse() + if config == "" { // 优先级: 命令行 > 环境变量 > 默认值 + if configEnv := os.Getenv(global.GVA_CONFIG.System.ConfigEnv); configEnv == "" { + config = defaultConfigFile + fmt.Printf("您正在使用config的默认值,config的路径为%v\n", defaultConfigFile) + } else { + config = configEnv + fmt.Printf("您正在使用GVA_CONFIG环境变量,config的路径为%v\n", config) + } + } else { + fmt.Printf("您正在使用命令行的-c参数传递的值,config的路径为%v\n", config) } - - v.SetConfigFile(configFile) + v := viper.New() + v.SetConfigFile(config) err := v.ReadInConfig() if err != nil { panic(fmt.Errorf("Fatal error config file: %s \n", err))