-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.go
54 lines (47 loc) · 1.08 KB
/
init.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
package global
import (
"flag"
"fmt"
"log"
"strings"
"sync"
"github.com/fsnotify/fsnotify"
"github.com/ic3network/mccs-alpha-api/util/l"
"github.com/spf13/viper"
)
var (
once = new(sync.Once)
configName = flag.String("config", "development", "config file name, default is development")
ShowVersionInfo = flag.Bool("v", false, "show version info or not")
)
func Init() {
once.Do(func() {
if !flag.Parsed() {
flag.Parse()
}
if err := initConfig(); err != nil {
panic(fmt.Errorf("initconfig failed: %s \n", err))
}
watchConfig()
l.Init(viper.GetString("env"))
})
}
func initConfig() error {
viper.SetConfigName(*configName)
viper.AddConfigPath("configs")
viper.AddConfigPath(App.RootDir + "/configs")
viper.SetConfigType("yaml")
viper.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
if err := viper.ReadInConfig(); err != nil {
return err
}
return nil
}
func watchConfig() {
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
log.Printf("Config file changed: %s", e.Name)
})
}