-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
101 lines (81 loc) · 1.98 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
93
94
95
96
97
98
99
100
101
package main
import (
"fmt"
"github.com/astaxie/beego/config"
"github.com/kataras/iris/core/errors"
)
var (
AppConfig *Config
)
type Config struct {
LogLevel string
LogPath string
ChanSize int
kafkaAddr string
collectConf []CollectConf
etcdAddrr string
configKey string
}
func LoadConf(confType, filename string) (err error) {
// 按照指定的格式读取配置文件
conf, err := config.NewConfig(confType, filename)
if err != nil {
fmt.Printf("new config failed, err:%v\n", err)
return
}
// 获得配置文件的选项
AppConfig = &Config{}
AppConfig.LogLevel = conf.String("logs::log_level")
if len(AppConfig.LogLevel) == 0 {
AppConfig.LogLevel = "debug"
}
AppConfig.LogPath = conf.String("logs::log_path")
if len(AppConfig.LogPath) == 0 {
AppConfig.LogPath = "./logs"
}
// 队列大小,或者管道大小
AppConfig.ChanSize, err = conf.Int("collect::chan_size")
if err != nil {
AppConfig.ChanSize = 100
}
// kafka地址配置
AppConfig.kafkaAddr = conf.String("kafka::server_addr")
if len(AppConfig.kafkaAddr) == 0 {
err = fmt.Errorf("invalid kafka addr")
return
//AppConfig.kafkaAddr = "172.24.1.1"
}
// etcd 地址匹配值
AppConfig.etcdAddrr = conf.String("etcd::addr")
if len(AppConfig.etcdAddrr) == 0 {
err = fmt.Errorf("invalid etcd addr")
return
}
// etcd config key
AppConfig.configKey = conf.String("etcd::configKey")
if len(AppConfig.configKey) == 0 {
err = fmt.Errorf("invalid etcd configKey")
return
}
err = LoadCollectConf(conf)
if err != nil {
fmt.Printf("load collect conf failed, err %v\n", err)
return
}
return
}
func LoadCollectConf(conf config.Configer) (err error) {
var cc CollectConf
cc.LogPath = conf.String("collect::log_path")
if len(cc.LogPath) == 0 {
errors.New("invaild collect::log_path")
return
}
cc.Topic = conf.String("collect::topic")
if len(cc.Topic) == 0 {
errors.New("invaild collect::topic")
return
}
AppConfig.collectConf = append(AppConfig.collectConf, cc)
return
}