@@ -3,55 +3,26 @@ package core
3
3
import (
4
4
"flag"
5
5
"fmt"
6
- "github.com/flipped-aurora/gin-vue-admin/server/core/internal"
7
- "github.com/gin-gonic/gin"
8
6
"os"
9
7
"path/filepath"
10
8
9
+ "github.com/flipped-aurora/gin-vue-admin/server/core/internal"
10
+ "github.com/flipped-aurora/gin-vue-admin/server/global"
11
11
"github.com/fsnotify/fsnotify"
12
+ "github.com/gin-gonic/gin"
12
13
"github.com/spf13/viper"
13
-
14
- "github.com/flipped-aurora/gin-vue-admin/server/global"
15
14
)
16
15
17
- // Viper //
18
- // 优先级: 命令行 > 环境变量 > 默认值
19
- // Author [SliverHorn](https://github.com/SliverHorn)
20
- func Viper (path ... string ) * viper.Viper {
21
- var config string
22
-
23
- if len (path ) == 0 {
24
- flag .StringVar (& config , "c" , "" , "choose config file." )
25
- flag .Parse ()
26
- if config == "" { // 判断命令行参数是否为空
27
- if configEnv := os .Getenv (internal .ConfigEnv ); configEnv == "" { // 判断 internal.ConfigEnv 常量存储的环境变量是否为空
28
- switch gin .Mode () {
29
- case gin .DebugMode :
30
- config = internal .ConfigDefaultFile
31
- case gin .ReleaseMode :
32
- config = internal .ConfigReleaseFile
33
- case gin .TestMode :
34
- config = internal .ConfigTestFile
35
- }
36
- fmt .Printf ("您正在使用gin模式的%s环境名称,config的路径为%s\n " , gin .Mode (), config )
37
- } else { // internal.ConfigEnv 常量存储的环境变量不为空 将值赋值于config
38
- config = configEnv
39
- fmt .Printf ("您正在使用%s环境变量,config的路径为%s\n " , internal .ConfigEnv , config )
40
- }
41
- } else { // 命令行参数不为空 将值赋值于config
42
- fmt .Printf ("您正在使用命令行的-c参数传递的值,config的路径为%s\n " , config )
43
- }
44
- } else { // 函数传递的可变参数的第一个值赋值于config
45
- config = path [0 ]
46
- fmt .Printf ("您正在使用func Viper()传递的值,config的路径为%s\n " , config )
47
- }
16
+ // Viper 配置
17
+ func Viper () * viper.Viper {
18
+ config := getConfigPath ()
48
19
49
20
v := viper .New ()
50
21
v .SetConfigFile (config )
51
22
v .SetConfigType ("yaml" )
52
23
err := v .ReadInConfig ()
53
24
if err != nil {
54
- panic (fmt .Errorf ("Fatal error config file: %s \n " , err ))
25
+ panic (fmt .Errorf ("fatal error config file: %w " , err ))
55
26
}
56
27
v .WatchConfig ()
57
28
@@ -62,10 +33,44 @@ func Viper(path ...string) *viper.Viper {
62
33
}
63
34
})
64
35
if err = v .Unmarshal (& global .GVA_CONFIG ); err != nil {
65
- panic (err )
36
+ panic (fmt . Errorf ( "fatal error unmarshal config: %w" , err ) )
66
37
}
67
38
68
39
// root 适配性 根据root位置去找到对应迁移位置,保证root路径有效
69
40
global .GVA_CONFIG .AutoCode .Root , _ = filepath .Abs (".." )
70
41
return v
71
42
}
43
+
44
+ // getConfigPath 获取配置文件路径, 优先级: 命令行 > 环境变量 > 默认值
45
+ func getConfigPath () (config string ) {
46
+ // `-c` flag parse
47
+ flag .StringVar (& config , "c" , "" , "choose config file." )
48
+ flag .Parse ()
49
+ if config != "" { // 命令行参数不为空 将值赋值于config
50
+ fmt .Printf ("您正在使用命令行的 '-c' 参数传递的值, config 的路径为 %s\n " , config )
51
+ return
52
+ }
53
+ if env := os .Getenv (internal .ConfigEnv ); env != "" { // 判断环境变量 GVA_CONFIG
54
+ config = env
55
+ fmt .Printf ("您正在使用 %s 环境变量, config 的路径为 %s\n " , internal .ConfigEnv , config )
56
+ return
57
+ }
58
+
59
+ switch gin .Mode () { // 根据 gin 模式文件名
60
+ case gin .DebugMode :
61
+ config = internal .ConfigDebugFile
62
+ case gin .ReleaseMode :
63
+ config = internal .ConfigReleaseFile
64
+ case gin .TestMode :
65
+ config = internal .ConfigTestFile
66
+ }
67
+ fmt .Printf ("您正在使用 gin 的 %s 模式运行, config 的路径为 %s\n " , gin .Mode (), config )
68
+
69
+ _ , err := os .Stat (config )
70
+ if err != nil || os .IsNotExist (err ) {
71
+ config = internal .ConfigDefaultFile
72
+ fmt .Printf ("配置文件路径不存在, 使用默认配置文件路径: %s\n " , config )
73
+ }
74
+
75
+ return
76
+ }
0 commit comments