-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
79 lines (66 loc) · 1.59 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package config
import (
"github.com/fsnotify/fsnotify"
"github.com/illuminatingKong/kongming-kit/base/configx"
"github.com/illuminatingKong/kongming-kit/base/logx"
"github.com/illuminatingKong/kongming-kit/base/logx/logrusx"
"github.com/spf13/viper"
)
type ConfigsDir string
type ConfigName string
type ConfigType string
type Config struct {
ConfigsDir ConfigsDir
ConfigName ConfigName
Type ConfigType
Logger logx.Logger
*viper.Viper
}
var formatter logrusx.JsonFormatter
func NewFile(options ...Option) configx.Conf {
var c = &Config{
Logger: logrusx.New(logrusx.WithFormatter(formatter)),
Viper: viper.New(),
}
for _, opt := range options {
processOptions(c, opt.get())
}
return c
}
func WithConfigsDir(ConfigsDir ConfigsDir) Option {
return &option{ConfigsDir}
}
func WithConfigName(ConfigName ConfigName) Option {
return &option{ConfigName}
}
func WithConfigType(ConfigType ConfigType) Option {
return &option{ConfigType}
}
func processOptions(c *Config, opt interface{}) {
switch val := opt.(type) {
case ConfigsDir:
c.ConfigsDir = val
case ConfigName:
c.ConfigName = val
case ConfigType:
c.Type = val
}
}
func (c *Config) Load() error {
c.Viper.SetConfigName(string(c.ConfigName))
c.Viper.SetConfigType(string(c.Type))
c.SetConfigxPath(string(c.ConfigsDir))
if err := c.Viper.ReadInConfig(); err != nil { // viper解析配置文件
panic(err)
}
return nil
}
func (c *Config) Watch() bool {
var v bool
c.Viper.WatchConfig()
c.Viper.OnConfigChange(func(e fsnotify.Event) {
c.Logger.Infof("Config file changed: %+v", e.Name)
v = true
})
return v
}