File tree Expand file tree Collapse file tree 3 files changed +21
-16
lines changed Expand file tree Collapse file tree 3 files changed +21
-16
lines changed Original file line number Diff line number Diff line change @@ -78,6 +78,7 @@ system:
78
78
db-type : " mysql" # support mysql/postgresql/sqlite/sqlserver
79
79
need-init-data : false
80
80
error-to-email : false
81
+ config-env : " GVA_CONFIG"
81
82
82
83
# captcha configuration
83
84
captcha :
Original file line number Diff line number Diff line change @@ -22,7 +22,8 @@ type System struct {
22
22
Addr int `mapstructure:"addr" json:"addr" yaml:"addr"`
23
23
DbType string `mapstructure:"db-type" json:"dbType" yaml:"db-type"`
24
24
NeedInitData bool `mapstructure:"need-init-data" json:"needInitData" yaml:"need-init-data"`
25
- ErrorToEmail bool `mapstructure:"error-to-email" json:"errorToEmail" yaml:"error-to-email"`
25
+ ErrorToEmail bool `mapstructure:"error-to-email" json:"errorToEmail" yaml:"error-to-email"`
26
+ ConfigEnv string `mapstructure:"config-env" json:"configEnv" yaml:"config-env"`
26
27
}
27
28
28
29
type JWT struct {
Original file line number Diff line number Diff line change 1
1
package core
2
2
3
3
import (
4
+ "flag"
4
5
"fmt"
5
6
"gin-vue-admin/global"
6
7
_ "gin-vue-admin/packfile"
7
8
"github.com/fsnotify/fsnotify"
8
9
"github.com/spf13/viper"
9
- "github.com/spf13/pflag "
10
+ "os "
10
11
)
11
12
13
+ var config string
14
+
12
15
const defaultConfigFile = "config.yaml"
13
16
14
17
func init () {
15
- pflag . StringP ( "configFile" , "c" , "" , "choose config file." )
16
- pflag .Parse ()
17
-
18
- // 优先级: 命令行 > 环境变量 > 默认值
19
- v := viper . New ()
20
- v . BindPFlags ( pflag . CommandLine )
21
- v . SetEnvPrefix ( "gva" )
22
- v . BindEnv ( "configFile" ) // GVA_CONFIGFILE
23
-
24
- configFile := v . GetString ( "configFile" )
25
- if configFile == "" {
26
- configFile = defaultConfigFile
18
+ flag . StringVar ( & config , "c" , "" , "choose config file." )
19
+ flag .Parse ()
20
+ if config == "" { // 优先级: 命令行 > 环境变量 > 默认值
21
+ if configEnv := os . Getenv ( global . GVA_CONFIG . System . ConfigEnv ); configEnv == "" {
22
+ config = defaultConfigFile
23
+ fmt . Printf ( "您正在使用config的默认值,config的路径为%v \n " , defaultConfigFile )
24
+ } else {
25
+ config = configEnv
26
+ fmt . Printf ( "您正在使用GVA_CONFIG环境变量,config的路径为%v \n " , config )
27
+ }
28
+ } else {
29
+ fmt . Printf ( "您正在使用命令行的-c参数传递的值,config的路径为%v \n " , config )
27
30
}
28
-
29
- v .SetConfigFile (configFile )
31
+ v := viper . New ()
32
+ v .SetConfigFile (config )
30
33
err := v .ReadInConfig ()
31
34
if err != nil {
32
35
panic (fmt .Errorf ("Fatal error config file: %s \n " , err ))
You can’t perform that action at this time.
0 commit comments