|
|
@@ -5,6 +5,7 @@ import (
|
|
5
|
5
|
"fmt"
|
|
6
|
6
|
"github.com/fsnotify/fsnotify"
|
|
7
|
7
|
"github.com/spf13/viper"
|
|
|
8
|
+ "strings"
|
|
8
|
9
|
)
|
|
9
|
10
|
|
|
10
|
11
|
const (
|
|
|
@@ -26,7 +27,7 @@ type Config struct {
|
|
26
|
27
|
}
|
|
27
|
28
|
|
|
28
|
29
|
// Init 初始化配置
|
|
29
|
|
-func Init(rowVal interface{}, cb func()) *viper.Viper {
|
|
|
30
|
+func Init(rowVal interface{}) *viper.Viper {
|
|
30
|
31
|
var config string
|
|
31
|
32
|
flag.StringVar(&config, "c", "", "choose config file.")
|
|
32
|
33
|
flag.Parse()
|
|
|
@@ -34,6 +35,9 @@ func Init(rowVal interface{}, cb func()) *viper.Viper {
|
|
34
|
35
|
config = DefaultConfig
|
|
35
|
36
|
}
|
|
36
|
37
|
v := viper.New()
|
|
|
38
|
+ v.AutomaticEnv()
|
|
|
39
|
+ v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
40
|
+ v.SetEnvPrefix("RUSTDESK_API")
|
|
37
|
41
|
v.SetConfigFile(config)
|
|
38
|
42
|
v.SetConfigType("yaml")
|
|
39
|
43
|
err := v.ReadInConfig()
|
|
|
@@ -47,10 +51,19 @@ func Init(rowVal interface{}, cb func()) *viper.Viper {
|
|
47
|
51
|
if err2 := v.Unmarshal(rowVal); err2 != nil {
|
|
48
|
52
|
fmt.Println(err2)
|
|
49
|
53
|
}
|
|
50
|
|
- cb()
|
|
51
|
54
|
})
|
|
52
|
55
|
if err := v.Unmarshal(rowVal); err != nil {
|
|
53
|
56
|
fmt.Println(err)
|
|
54
|
57
|
}
|
|
55
|
58
|
return v
|
|
56
|
59
|
}
|
|
|
60
|
+
|
|
|
61
|
+// ReadEnv 读取环境变量
|
|
|
62
|
+func ReadEnv(rowVal interface{}) *viper.Viper {
|
|
|
63
|
+ v := viper.New()
|
|
|
64
|
+ v.AutomaticEnv()
|
|
|
65
|
+ if err := v.Unmarshal(rowVal); err != nil {
|
|
|
66
|
+ fmt.Println(err)
|
|
|
67
|
+ }
|
|
|
68
|
+ return v
|
|
|
69
|
+}
|