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