-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.go
40 lines (32 loc) · 838 Bytes
/
load.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
package config
import (
"fmt"
"strings"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"go.uber.org/zap"
)
func Load(configFile string) (*AppConfig, error) {
viper.SetConfigFile(configFile)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
return nil, fmt.Errorf("viper.ReadInConfig -> %w", err)
}
var conf *AppConfig
if err := viper.Unmarshal(&conf); err != nil {
return nil, fmt.Errorf("viper.Unmarshal -> %w", err)
}
if err := conf.validateConfig(); err != nil {
return nil, fmt.Errorf("conf.validateConfig -> %w", err)
}
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
zap.L().Info(
"config file changed",
zap.String("fileName", e.Name),
zap.Any("operation", e.Op),
)
})
return conf, nil
}