-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
68 lines (59 loc) · 1.11 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
package config
import (
"fmt"
"log"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
type (
// redis struct {
// Host string `default:"192.168.3.3"`
// Port string `default:"6379"`
// Select int `default:"0"`
// Password string
// Prefix string
// }
mysql struct {
Port string
Host string
Database string
Username string
Password string
Dump bool
}
)
type Config struct {
App struct {
Name string
Mode string
Port string
Cookie string
Debug bool
}
Mysql mysql
}
func init() {
Viper, _ = InitConfig()
}
func InitConfig() (*viper.Viper, error) {
v := viper.New()
v.AddConfigPath(".")
v.SetConfigType("yaml")
v.SetConfigName("config.yaml")
if err := v.ReadInConfig(); err == nil {
log.Printf("use config file -> %s\n", v.ConfigFileUsed())
} else {
return nil, err
}
v.WatchConfig()
v.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("config file changed:", e.Name)
if err := v.Unmarshal(&Conf); err != nil {
fmt.Println(err)
}
})
if err := v.Unmarshal(&Conf); err != nil {
fmt.Println(err)
}
return v, nil
}