-
Notifications
You must be signed in to change notification settings - Fork 30
/
config.go
92 lines (84 loc) · 2.28 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
80
81
82
83
84
85
86
87
88
89
90
91
92
package config
import (
"fmt"
"github.com/fsnotify/fsnotify"
logger "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/wuchunfu/IpProxyPool/util/fileutil"
"os"
)
type System struct {
AppName string `yaml:"appName"`
HttpAddr string `yaml:"httpAddr"`
HttpPort string `yaml:"httpPort"`
}
type Database struct {
DbType string `yaml:"dbType"`
Host string `yaml:"host"`
Port int `yaml:"port"`
DbName string `yaml:"dbName"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Prefix string `yaml:"prefix"`
Charset string `yaml:"charset"`
MaxIdleConns int `yaml:"maxIdleConns"`
MaxOpenConns int `yaml:"maxOpenConns"`
Level string `yaml:"level"`
SslMode string `yaml:"sslMode"`
TimeZone string `yaml:"timeZone"`
}
type Log struct {
FilePath string `yaml:"filePath"`
FileName string `yaml:"fileName"`
Level string `yaml:"level"`
Mode string `yaml:"mode"`
}
type YamlSetting struct {
System System `yaml:"system"`
Database Database `yaml:"database"`
Log Log `yaml:"log"`
}
var (
Vip = viper.New()
ConfigFile = ""
ServerSetting = new(YamlSetting)
)
// InitConfig reads in config file and ENV variables if set.
func InitConfig() {
if ConfigFile != "" {
if !fileutil.PathExists(ConfigFile) {
logger.Errorf("No such file or directory: %s", ConfigFile)
os.Exit(-1)
} else {
// Use config file from the flag.
Vip.SetConfigFile(ConfigFile)
Vip.SetConfigType("yaml")
}
} else {
logger.Errorf("Could not find config file: %s", ConfigFile)
os.Exit(-1)
}
// If a config file is found, read it in.
err := Vip.ReadInConfig()
if err != nil {
logger.Errorf("Failed to get config file: %s", ConfigFile)
}
Vip.WatchConfig()
Vip.OnConfigChange(func(e fsnotify.Event) {
logger.Infof("Config file changed: %s\n", e.Name)
fmt.Printf("Config file changed: %s\n", e.Name)
ServerSetting = GetConfig(Vip)
})
Vip.AllSettings()
ServerSetting = GetConfig(Vip)
}
// 解析配置文件,反序列化
func GetConfig(vip *viper.Viper) *YamlSetting {
setting := new(YamlSetting)
// 解析配置文件,反序列化
if err := vip.Unmarshal(setting); err != nil {
logger.Errorf("Unmarshal yaml faild: %s", err)
os.Exit(-1)
}
return setting
}