-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
85 lines (74 loc) · 1.68 KB
/
options.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
package mqtt
import (
"fmt"
"github.com/golang-io/mqtt/packet"
"github.com/golang-io/requests"
)
type Listen struct {
URL string `yaml:"url"`
CertFile string `yaml:"certFile"`
KeyFile string `yaml:"keyFile"`
}
type config struct {
HTTP Listen `json:"HTTP"`
MQTT Listen `json:"MQTT"`
MQTTs Listen `json:"MQTTs"`
WebSocket Listen `json:"Websocket"`
WebSockets Listen `json:"Websockets"`
Auth map[string]string `json:"Auth"`
}
func (c *config) GetAuth(username string) (string, bool) {
password, ok := c.Auth[username]
return password, ok
}
var CONFIG = &config{
Auth: map[string]string{
"": "",
"root": "admin",
},
}
type Options struct {
URL string // client used
ClientID string
Version byte
Subscriptions []packet.Subscription
}
type Option func(*Options)
func newOptions(opts ...Option) Options {
options := Options{
URL: "mqtt://127.0.0.1:1883",
ClientID: "mqtt-" + requests.GenId(),
Version: packet.VERSION311,
}
for _, o := range opts {
o(&options)
}
return options
}
func URL(url string) Option {
return func(o *Options) {
o.URL = url
}
}
func Subscription(subscription ...packet.Subscription) Option {
return func(o *Options) {
o.Subscriptions = append(o.Subscriptions, subscription...)
}
}
func Version[T ~string | ~byte](version T) Option {
return func(o *Options) {
switch v := any(version).(type) {
case byte:
o.Version = v
case string:
switch v {
case "5.0.0":
o.Version = packet.VERSION500
case "3.1.1":
o.Version = packet.VERSION311
default:
panic(fmt.Errorf("version = %s not support", v))
}
}
}
}