forked from DrmagicE/gmqtt
/
config.go
103 lines (92 loc) · 2.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
package run
import (
"fmt"
"github.com/gin-gonic/gin"
"gopkg.in/yaml.v2"
"io/ioutil"
)
// ProtocolMQTT represents the MQTT protocol under TCP server
const ProtocolMQTT = "mqtt"
// ProtocolWebsocket represents the MQTT protocol under websocket server.
const ProtocolWebsocket = "websocket"
//Default configration
const (
DefaultDeliveryRetryInterval = 20
DefaultQueueQos0Messages = true
DefaultMaxInflightMessages = 20
DefaultLogging = false
DefaultMaxMsgQueueMessages = 2048
)
// Config represents the configuration options.
type Config struct {
DeliveryRetryInterval int64 `yaml:"delivery_retry_interval"`
QueueQos0Messages bool `yaml:"queue_qos0_messages"`
MaxInflightMessages int `yaml:"max_inflight_messages"`
MaxMsgQueueMessages int `yaml:"max_msgqueue_messages"`
ProfileConfig ProfileConfig `yaml:"profile"`
Listener []ListenerConfig `yaml:"listener,flow"`
Logging bool `yaml:"logging"`
HttpServerConfig HttpServerConfig `yaml:"http_server"`
}
// ProfileConfig represents the server profile configuration.
type ProfileConfig struct {
CPUProfile string `yaml:"cpu"`
MemProfile string `yaml:"mem"`
}
// HttpServerConfig represents the REST server configuration.
type HttpServerConfig struct {
Addr string `yaml:"addr"`
User gin.Accounts `yaml:"user"`
}
// ListenerConfig represents the tcp server configuration.
type ListenerConfig struct {
Protocol string `yaml:"protocol"`
Addr string `yaml:"addr"`
CertFile string `yaml:"certfile"`
KeyFile string `yaml:"keyfile"`
}
// Validate validate the Config, returns error if it is invalid.
func (c *Config) Validate() error {
for _, v := range c.Listener {
if v.Protocol != ProtocolMQTT && v.Protocol != ProtocolWebsocket {
return fmt.Errorf("invalid protocol name '%s',expect 'mqtt' or 'websocket'", v.Protocol)
}
if v.KeyFile != "" && v.CertFile == "" {
return fmt.Errorf("invalid tls/ssl configration, 'certfile missing'")
}
if v.KeyFile == "" && v.CertFile != "" {
return fmt.Errorf("invalid tls/ssl configration, 'keyfile' missing")
}
if v.Addr == "" {
return fmt.Errorf("addr missing")
}
}
return nil
}
// NewConfig returns the default Config instance.
func NewConfig() *Config {
return &Config{
DeliveryRetryInterval: DefaultDeliveryRetryInterval,
QueueQos0Messages: DefaultQueueQos0Messages,
MaxInflightMessages: DefaultMaxInflightMessages,
MaxMsgQueueMessages: DefaultMaxMsgQueueMessages,
Logging: DefaultLogging,
}
}
// FromConfigFile loads the configuration from a yaml file
func (c *Config) FromConfigFile(fpath string) error {
bs, err := ioutil.ReadFile(fpath)
if err != nil {
return err
}
err = yaml.Unmarshal(bs, c)
if err != nil {
return err
}
if len(c.Listener) == 0 {
c.Listener = make([]ListenerConfig, 1)
c.Listener[0].Protocol = ProtocolMQTT
c.Listener[0].Addr = ":1883"
}
return nil
}