/
configuration.go
188 lines (164 loc) · 4.69 KB
/
configuration.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
187
188
// Copyright 2014, Shuhei Tanuma. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
package configuration
import (
"fmt"
"github.com/BurntSushi/toml"
"io/ioutil"
"runtime"
"strconv"
"strings"
)
type Config struct {
Server Server `toml:"server"`
Engine Engine `toml:"engine"`
Bridge Bridge `toml:"bridge"`
}
type Engine struct {
QueueSize int `toml:"queue_size"`
AcceptorCount string `toml:"acceptor_count"`
LockPoolSize int `toml:"lock_pool_size"`
EnableSys bool `toml:"enable_sys"`
FanoutWorkerCount string `toml:"fanout_worker_count"`
AllowAnonymous bool `toml:"allow_anonymous"`
Authenticators []Authenticator `toml:"authenticator"`
EnablePermission bool `toml:"enable_permission"`
}
type Authenticator struct {
Type string `toml:"type"`
}
type Bridge struct {
Address string `toml:"address"`
Port int `toml:"port"`
CleanSession bool `toml:"clean_session"`
ClientId string `toml:"client_id"`
KeepaliveInterval int `toml:"keepalive_interval"`
Connection string `toml:"connection"`
Type string `toml:"type"`
}
type Server struct {
User string `toml:"user"`
LogFile string `toml:"log_file"`
LogLevel string `toml:"log_level"`
PidFile string `toml:"pid_file"`
BindAddress string `toml:"bind_address"`
Port int `toml:"port"`
EnableTls bool `toml:"enable_tls"`
TlsPort int `toml:"tls_port"`
Keyfile string `toml:"keyfile"`
Cafile string `toml:"cafile"`
Certfile string `toml:"certfile"`
Socket string `toml:"socket"`
HttpPort int `toml:"http_port"`
WebSocketMount string `toml:"websocket_mount"`
HttpDebug bool `toml:"http_debug"`
MaxInflightMessages int `toml:"max_inflight_messages"`
MaxQueuedMessages int `toml:"max_queued_messages"`
RetryInterval int `toml:"retry_interval"`
MessageSizeLimit int `toml:"message_size_limit"`
}
func (self *Config) GetQueueSize() int {
return self.Engine.QueueSize
}
func (self *Config) GetFanoutWorkerCount() int {
if strings.ToLower(self.Engine.FanoutWorkerCount) == "cpu" {
return runtime.NumCPU()
} else {
v, e := strconv.ParseInt(self.Engine.FanoutWorkerCount, 10, 64)
if e != nil {
return 1
}
return int(v)
}
}
func (self *Config) GetLockPoolSize() int {
return self.Engine.LockPoolSize
}
func (self *Config) GetAcceptorCount() int {
if strings.ToLower(self.Engine.AcceptorCount) == "cpu" {
return runtime.NumCPU()
} else {
v, e := strconv.ParseInt(self.Engine.AcceptorCount, 10, 64)
if e != nil {
return 1
}
return int(v)
}
}
func (self *Config) GetListenAddress() string {
if self.Server.Port <= 0 {
return ""
}
return fmt.Sprintf("%s:%d", self.Server.BindAddress, self.Server.Port)
}
func (self *Config) GetTlsListenAddress() string {
if self.Server.TlsPort <= 0 {
return ""
}
return fmt.Sprintf("%s:%d", self.Server.BindAddress, self.Server.TlsPort)
}
func (self *Config) GetSSLListenAddress() string {
if self.Server.Port <= 0 {
return ""
}
return fmt.Sprintf("%s:8883", self.Server.BindAddress)
}
func (self *Config) GetSocketAddress() string {
return self.Server.Socket
}
func (self *Config) GetAuthenticators() []Authenticator {
return self.Engine.Authenticators
}
func DefaultConfiguration() *Config {
return &Config{
Engine: Engine{
QueueSize: 8192,
AcceptorCount: "cpu",
FanoutWorkerCount: "cpu",
LockPoolSize: 64,
EnableSys: true,
AllowAnonymous: true,
EnablePermission: false,
},
Server: Server{
User: "momonga",
LogFile: "stdout",
LogLevel: "debug",
PidFile: "",
BindAddress: "localhost",
Port: 1883,
Socket: "",
HttpPort: 0,
WebSocketMount: "/mqtt",
HttpDebug: true,
MaxInflightMessages: 10000,
MaxQueuedMessages: 10000,
RetryInterval: 20,
MessageSizeLimit: 8192,
EnableTls: false,
TlsPort: 8883,
},
}
}
func LoadConfiguration(configFile string) (*Config, error) {
config := DefaultConfiguration()
data, err := ioutil.ReadFile(configFile)
if err != nil {
return config, err
}
if _, err2 := toml.Decode(string(data), config); err != nil {
return config, err2
}
return config, nil
}
func LoadConfigurationTo(configFile string, to *Config) error {
data, err := ioutil.ReadFile(configFile)
if err != nil {
return err
}
if _, err2 := toml.Decode(string(data), to); err != nil {
return err2
}
return nil
}