forked from Terry-Mao/gopush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
72 lines (65 loc) · 2 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
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"runtime"
)
var (
ConfFile string
)
func init() {
flag.StringVar(&ConfFile, "c", "./gopush.conf", " set gopush config file path")
}
type Config struct {
Addr string `json:"addr"`
Port int `json:"port"`
Pprof int `json:"pprof"`
PprofAddr string `json:"pprof_addr"`
PprofPort int `json:"pprof_port"`
PubAddr string `json:"pub_addr"`
PubPort int `json:"pub_port"`
LongpollingTimeout int `json:"longpolling_timeout"`
MessageTimeout int `json:"message_timeout"`
Log string `json:"log"`
RedisNetwork string `json:"redis_network"`
RedisAddr string `json:"redis_addr"`
RedisTimeout int `json:"redis_timeout"`
RedisPoolSize int `json:"redis_poolsize"`
RedisMQSize int `json:"redis_mqsize"`
MaxProcs int `json:"max_procs"`
MaxSubscriberPerKey int `json:"max_subscriber_per_key"`
TCPKeepAlive int `json:"tcp_keepalive"`
}
func InitConfig(file string) (*Config, error) {
c, err := ioutil.ReadFile(file)
if err != nil {
Log.Printf("ioutil.ReadFile(\"%s\") failed (%s)", file, err.Error())
return nil, err
}
cf := &Config{
Addr: "localhost",
Port: 8080,
PprofAddr: "localhost",
PprofPort: 8080,
PubAddr: "localhost",
PubPort: 8080,
Pprof: 1,
LongpollingTimeout: 300,
MessageTimeout: 7200,
Log: "./gopush.log",
RedisNetwork: "tcp",
RedisAddr: "localhost:6379",
RedisTimeout: 28800,
RedisPoolSize: 50,
RedisMQSize: 20,
MaxSubscriberPerKey: 0, // no limit
MaxProcs: runtime.NumCPU(),
TCPKeepAlive: 1,
}
if err = json.Unmarshal(c, cf); err != nil {
Log.Printf("json.Unmarshal(\"%s\", cf) failed (%s)", string(c), err.Error())
return nil, err
}
return cf, nil
}