-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
47 lines (36 loc) · 1.02 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package config
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
type Config struct {
Application Application `mapstructure:"application" json:"application" yaml:"application"`
Jwt Jwt `mapstructure:"jwt" json:"jwt" yaml:"jwt"`
Database Database `mapstructure:"database" json:"database" yaml:"database"`
Redis Redis `mapstructure:"redis" json:"redis" yaml:"redis"`
Logger Logger `mapstructure:"logger" json:"logger" yaml:"logger"`
SMS SMS `mapstructure:"sms" json:"sms" yaml:"sms"`
}
var (
OSConfig *Config
)
// 载入配置文件
func Setup(path string) {
v := viper.New()
v.SetConfigFile(path)
v.AutomaticEnv()
if err := v.ReadInConfig(); err != nil {
fmt.Println(err)
}
v.WatchConfig()
v.OnConfigChange(func(event fsnotify.Event) {
fmt.Println("config file changed:", event.Name)
if err := v.Unmarshal(&OSConfig); err != nil {
fmt.Println(err)
}
})
if err := v.Unmarshal(&OSConfig); err != nil {
fmt.Println(err)
}
}