-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
43 lines (39 loc) · 915 Bytes
/
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
package config
import (
log "github.com/sirupsen/logrus"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
var (
Port string
Database string
Environment string
)
type Config struct {
Port string `yaml:"port"`
Database string `yaml:"database"`
Environment string `yaml:"environment"`
}
func InitConfig(cfile string) {
var config Config
v := viper.New()
v.SetConfigFile(cfile)
v.SetConfigType("yaml")
err := v.ReadInConfig()
if err != nil {
log.Panicf("Fatal error config file: %s \n", err)
}
v.WatchConfig()
v.OnConfigChange(func(e fsnotify.Event) {
log.Warnf("Config file changed: %v", e.Name)
if err := v.Unmarshal(&config); err != nil {
log.Panicf("Config file format error %v",err)
}
})
if err := v.Unmarshal(&config); err != nil {
log.Panicf("Config file format error %v",err)
}
Port = config.Port
Database = config.Database
Environment = config.Environment
}