forked from Terry-Mao/gopush-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
116 lines (106 loc) · 3.8 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright © 2014 Terry Mao, LiuDing All rights reserved.
// This file is part of gopush-cluster.
// gopush-cluster is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// gopush-cluster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with gopush-cluster. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"flag"
"fmt"
"github.com/Terry-Mao/goconf"
"github.com/golang/glog"
"runtime"
"time"
)
var (
Conf *Config
ConfFile string
)
func init() {
flag.StringVar(&ConfFile, "c", "./message.conf", " set message config file path")
}
// Config struct
type Config struct {
Addr []string `goconf:"base:addr:,"`
PKey string `goconf:"base:pkey"`
User string `goconf:"base:user"`
PidFile string `goconf:"base:pidfile"`
Dir string `goconf:"base:dir"`
MaxProc int `goconf:"base:maxproc"`
PprofBind []string `goconf:"base:pprof.bind:,"`
StorageType string `goconf:"storage:type"`
RedisIdleTimeout time.Duration `goconf:"redis:idletimeout:time"`
RedisMaxIdle int `goconf:"redis:maxidle"`
RedisMaxActive int `goconf:"redis:maxactive"`
RedisMaxStore int `goconf:"redis:maxstore"`
MySQLClean time.Duration `goconf:"mysql:clean:time"`
RedisSource map[string]string `goconf:"-"`
MySQLSource map[string]string `goconf:"-"`
// zookeeper
ZookeeperAddr []string `goconf:"zookeeper:addr:,"`
ZookeeperTimeout time.Duration `goconf:"zookeeper:timeout:time"`
ZookeeperPath string `goconf:"zookeeper:path"`
}
// Initialize config
func NewConfig(fileName string) (*Config, error) {
gconf := goconf.New()
if err := gconf.Parse(fileName); err != nil {
glog.Errorf("goconf.Parse(\"%s\") error(%v)", fileName, err)
return nil, err
}
conf := &Config{
Addr: []string{"localhost:8070"},
PKey: "gopushpkey",
User: "nobody nobody",
PidFile: "/tmp/gopush-cluster-message.pid",
Dir: "./",
MaxProc: runtime.NumCPU(),
PprofBind: []string{"localhost:8170"},
StorageType: "redis",
RedisIdleTimeout: 28800 * time.Second,
RedisMaxIdle: 50,
RedisMaxActive: 1000,
RedisMaxStore: 20,
RedisSource: make(map[string]string),
MySQLSource: make(map[string]string),
MySQLClean: 1 * time.Hour,
// zookeeper
ZookeeperAddr: []string{"localhost:2181"},
ZookeeperTimeout: 30 * time.Second,
ZookeeperPath: "/gopush-cluster-message",
}
if err := gconf.Unmarshal(conf); err != nil {
glog.Errorf("goconf.Unmarshal() error(%v)", err)
return nil, err
}
//Load redis addresses
redisAddrsSec := gconf.Get("redis.source")
if redisAddrsSec != nil {
for _, key := range redisAddrsSec.Keys() {
addr, err := redisAddrsSec.String(key)
if err != nil {
return nil, fmt.Errorf("config section:\"redis.addrs\" key:\"%s\" error(%v)", key, err)
}
conf.RedisSource[key] = addr
}
}
//Load mysql sources
dbSource := gconf.Get("mysql.source")
if dbSource != nil {
for _, key := range dbSource.Keys() {
source, err := dbSource.String(key)
if err != nil {
return nil, fmt.Errorf("config section:\"mysql.source\" key:\"%s\" error(%v)", key, err)
}
conf.MySQLSource[key] = source
}
}
return conf, nil
}