-
Notifications
You must be signed in to change notification settings - Fork 151
/
config.go
58 lines (48 loc) · 1.16 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
package g
import (
"fmt"
"log"
"sync"
"github.com/toolkits/file"
"gopkg.in/yaml.v2"
)
type WebConf struct {
Addrs []string `yaml:"addrs"`
Interval int `yaml:"interval"`
Timeout int `yaml:"timeout"`
}
type GlobalConfig struct {
Debug bool `yaml:"debug"`
IDC string `yaml:"idc"`
Worker int `yaml:"worker"`
Web *WebConf `yaml:"web"`
}
var (
Config *GlobalConfig
configLock = new(sync.RWMutex)
IP string
)
func Parse(cfg string) error {
if cfg == "" {
return fmt.Errorf("use -c to specify configuration file")
}
if !file.IsExist(cfg) {
return fmt.Errorf("configuration file %s is nonexistent", cfg)
}
configContent, err := file.ToTrimString(cfg)
if err != nil {
return fmt.Errorf("read configuration file %s fail %s", cfg, err.Error())
}
var c GlobalConfig
fmt.Printf("configContent:\n%s", configContent)
err = yaml.Unmarshal([]byte(configContent), &c)
if err != nil {
return fmt.Errorf("parse configuration file %s fail %s", cfg, err.Error())
}
configLock.Lock()
defer configLock.Unlock()
Config = &c
log.Println(Config)
log.Println("load configuration file", cfg, "successfully")
return nil
}