-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
45 lines (39 loc) · 1014 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
44
45
package main
import (
"github.com/spf13/viper"
"log"
)
type config struct {
Debug bool
Port int
Container *struct {
Nfs string
Image string
}
Aliyun *struct {
AccessKey string
SecretKey string
RegionId string
ZoneId string
SecurityGroupId string
VSwitchId string
}
}
var Config config
func initConfig() {
viper.SetConfigName("config") // name of config file (without extension)
viper.AddConfigPath("/etc/minestat/") // path to look for the config file in
viper.AddConfigPath("$HOME/.minestat") // call multiple times to add many search paths
viper.AddConfigPath(".") // optionally look for config in the working directory
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
log.Fatalf("读取配置文件失败, %v", err)
}
err = viper.Unmarshal(&Config)
if err != nil {
log.Fatalf("转换配置文件失败, %v", err)
}
if Config.Port == 0 {
Config.Port = 8080
}
}