forked from DrmagicE/gmqtt
/
config.go
239 lines (216 loc) · 5.95 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package config
import (
"fmt"
"io/ioutil"
"os"
"path"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/yaml.v2"
)
var (
defaultPluginConfig = make(map[string]Configuration)
)
// Configuration is the interface that enable the implementation to parse config from the global config file.
// Plugin admin and prometheus are two examples.
type Configuration interface {
// Validate validates the configuration.
// If returns error, the broker will not start.
Validate() error
// Unmarshaler defined how to unmarshal YAML into the config structure.
yaml.Unmarshaler
}
// RegisterDefaultPluginConfig registers the default configuration for the given plugin.
func RegisterDefaultPluginConfig(name string, config Configuration) {
if _, ok := defaultPluginConfig[name]; ok {
panic(fmt.Sprintf("duplicated default config for %s plugin", name))
}
defaultPluginConfig[name] = config
}
// DefaultConfig return the default configuration.
// If config file is not provided, gmqttd will start with DefaultConfig.
func DefaultConfig() Config {
c := Config{
Listeners: DefaultListeners,
MQTT: DefaultMQTTConfig,
API: DefaultAPI,
Log: LogConfig{
Level: "info",
Format: "text",
},
Plugins: make(pluginConfig),
Persistence: DefaultPersistenceConfig,
TopicAliasManager: DefaultTopicAliasManager,
}
for name, v := range defaultPluginConfig {
c.Plugins[name] = v
}
return c
}
var DefaultListeners = []*ListenerConfig{
{
Address: "0.0.0.0:1883",
TLSOptions: nil,
Websocket: nil,
},
{
Address: "0.0.0.0:8883",
Websocket: &WebsocketOptions{
Path: "/",
},
},
}
// LogConfig is use to configure the log behaviors.
type LogConfig struct {
// Level is the log level. Possible values: debug, info, warn, error
Level string `yaml:"level"`
// Format is the log format. Possible values: json, text
Format string `yaml:"format"`
// DumpPacket indicates whether to dump MQTT packet in debug level.
DumpPacket bool `yaml:"dump_packet"`
}
func (l LogConfig) Validate() error {
if l.Level != "debug" && l.Level != "info" && l.Level != "warn" && l.Level != "error" {
return fmt.Errorf("invalid log level: %s", l.Level)
}
if l.Format != "json" && l.Format != "text" {
return fmt.Errorf("invalid log format: %s", l.Format)
}
return nil
}
// pluginConfig stores the plugin default configuration, key by the plugin name.
// If the plugin has default configuration, it should call RegisterDefaultPluginConfig in it's init function to register.
type pluginConfig map[string]Configuration
func (p pluginConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
for _, v := range p {
err := unmarshal(v)
if err != nil {
return err
}
}
return nil
}
// Config is the configration for gmqttd.
type Config struct {
Listeners []*ListenerConfig `yaml:"listeners"`
API API `yaml:"api"`
MQTT MQTT `yaml:"mqtt,omitempty"`
GRPC GRPC `yaml:"gRPC"`
Log LogConfig `yaml:"log"`
PidFile string `yaml:"pid_file"`
ConfigDir string `yaml:"config_dir"`
Plugins pluginConfig `yaml:"plugins"`
// PluginOrder is a slice that contains the name of the plugin which will be loaded.
// Giving a correct order to the slice is significant,
// because it represents the loading order which affect the behavior of the broker.
PluginOrder []string `yaml:"plugin_order"`
Persistence Persistence `yaml:"persistence"`
TopicAliasManager TopicAliasManager `yaml:"topic_alias_manager"`
}
type GRPC struct {
Endpoint string `yaml:"endpoint"`
}
type TLSOptions struct {
// CACert is the trust CA certificate file.
CACert string `yaml:"cacert"`
// Cert is the path to certificate file.
Cert string `yaml:"cert"`
// Key is the path to key file.
Key string `yaml:"key"`
// Verify indicates whether to verify client cert.
Verify bool `yaml:"verify"`
}
type ListenerConfig struct {
Address string `yaml:"address"`
*TLSOptions `yaml:"tls"`
Websocket *WebsocketOptions `yaml:"websocket"`
}
type WebsocketOptions struct {
Path string `yaml:"path"`
}
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
type config Config
raw := config(DefaultConfig())
if err := unmarshal(&raw); err != nil {
return err
}
emptyMQTT := MQTT{}
if raw.MQTT == emptyMQTT {
raw.MQTT = DefaultMQTTConfig
}
if len(raw.Plugins) == 0 {
raw.Plugins = make(pluginConfig)
for name, v := range defaultPluginConfig {
raw.Plugins[name] = v
}
} else {
for name, v := range raw.Plugins {
if v == nil {
raw.Plugins[name] = defaultPluginConfig[name]
}
}
}
*c = Config(raw)
return nil
}
func (c Config) Validate() (err error) {
err = c.Log.Validate()
if err != nil {
return err
}
err = c.API.Validate()
if err != nil {
return err
}
err = c.MQTT.Validate()
if err != nil {
return err
}
err = c.Persistence.Validate()
if err != nil {
return err
}
for _, conf := range c.Plugins {
err := conf.Validate()
if err != nil {
return err
}
}
return nil
}
func ParseConfig(filePath string) (c Config, err error) {
if filePath == "" {
return DefaultConfig(), nil
}
b, err := ioutil.ReadFile(filePath)
if err != nil {
return c, err
}
c = DefaultConfig()
err = yaml.Unmarshal(b, &c)
if err != nil {
return c, err
}
c.ConfigDir = path.Dir(filePath)
err = c.Validate()
if err != nil {
return Config{}, err
}
return c, err
}
func (c Config) GetLogger(config LogConfig) (l *zap.Logger, err error) {
var logLevel zapcore.Level
err = logLevel.UnmarshalText([]byte(config.Level))
if err != nil {
return
}
var core zapcore.Core
if config.Format == "json" {
core = zapcore.NewCore(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), os.Stdout, logLevel)
}
if config.Format == "text" {
core = zapcore.NewCore(zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()), os.Stdout, logLevel)
}
zaplog := zap.New(core, zap.AddStacktrace(zap.ErrorLevel), zap.AddCaller())
return zaplog, nil
}