forked from liangdas/mqant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
executable file
·186 lines (166 loc) · 3.85 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2014 mqant Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package conf
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
)
var (
LenStackBuf = 1024
Conf = Config{}
)
func LoadConfig(Path string) {
// Read config.
if err := readFileInto(Path); err != nil {
panic(err)
}
if Conf.Rpc.MaxCoroutine == 0 {
Conf.Rpc.MaxCoroutine = 100
}
if Conf.Rpc.UDPMaxPacketSize == 0 {
Conf.Rpc.UDPMaxPacketSize = 4096
}
for _, module := range Conf.Module {
for _, ModuleSettings := range module {
if ModuleSettings.UDP != nil {
ModuleSettings.UDP.UDPMaxPacketSize = Conf.Rpc.UDPMaxPacketSize
}
}
}
}
type Config struct {
Log map[string]interface{}
Rpc Rpc
Module map[string][]*ModuleSettings
Mqtt Mqtt
Master Master
Settings map[string]interface{}
}
type Rpc struct {
UDPMaxPacketSize int //udp rpc 每一个包最大数据量 默认 4096
MaxCoroutine int //模块同时可以创建的最大协程数量默认是100
RpcExpired int //远程访问最后期限值 单位秒[默认5秒] 这个值指定了在客户端可以等待服务端多长时间来应答
LogSuccess bool //是否打印请求处理成功的日志
Log bool //是否打印RPC的日志
}
type Rabbitmq struct {
Uri string
Exchange string
ExchangeType string
Queue string
BindingKey string //
ConsumerTag string //消费者TAG
}
type Redis struct {
Uri string //redis://:[password]@[ip]:[port]/[db]
Queue string
}
type UDP struct {
Uri string //udp服务端监听ip 0.0.0.0:8080
Port int //端口
UDPMaxPacketSize int
}
type ModuleSettings struct {
Id string
Host string
ProcessID string
Settings map[string]interface{}
Rabbitmq *Rabbitmq
Redis *Redis
UDP *UDP
}
type Mqtt struct {
WirteLoopChanNum int // Should > 1 // 最大写入包队列缓存
ReadPackLoop int // 最大读取包队列缓存
ReadTimeout int // 读取超时
WriteTimeout int // 写入超时
}
type SSH struct {
Host string
Port int
User string
Password string
}
/**
host:port
*/
func (s *SSH) GetSSHHost() string {
return fmt.Sprintf("%s:%d", s.Host, s.Port)
}
type Process struct {
ProcessID string
Host string
//执行文件
Execfile string
//日志文件目录
//pid.nohup.log
//pid.access.log
//pid.error.log
LogDir string
//自定义的参数
Args map[string]interface{}
}
type Master struct {
Enable bool
WebRoot string
WebHost string
SSH []*SSH
Process []*Process
}
func (m *Master) GetSSH(host string) *SSH {
for _, ssh := range m.SSH {
if ssh.Host == host {
return ssh
}
}
return nil
}
func readFileInto(path string) error {
var data []byte
buf := new(bytes.Buffer)
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
r := bufio.NewReader(f)
for {
line, err := r.ReadSlice('\n')
if err != nil {
if len(line) > 0 {
buf.Write(line)
}
break
}
if !strings.HasPrefix(strings.TrimLeft(string(line), "\t "), "//") {
buf.Write(line)
}
}
data = buf.Bytes()
//fmt.Print(string(data))
return json.Unmarshal(data, &Conf)
}
// If read the file has an error,it will throws a panic.
func fileToStruct(path string, ptr *[]byte) {
data, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
*ptr = data
}