-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
74 lines (67 loc) · 2.1 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
package main
import (
"fmt"
"hash/crc32"
"math/rand"
"net"
"strings"
"github.com/Netflix/go-env"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
type Config struct {
MDNSInterface *net.Interface
MQTTClient mqtt.Client
PublishTopic string
SubscribeTopic string
}
type environmentConfig struct {
MQTTAddress string `env:"MQTT_ADDRESS,required=true"`
MQTTUsername string `env:"MQTT_USERNAME"`
MQTTPassword string `env:"MQTT_PASSWORD"`
PublishTopic string `env:"MQTT_PUBLISH_TOPIC"`
SubscribeTopic string `env:"MQTT_SUBSCRIBE_TOPIC"`
MDNSInterface string `env:"MDNS_INTERFACE,required=true"`
}
// NewConfigFromEnv creates a new config object with configuration loaded from environment variables.
func NewConfigFromEnv() (*Config, error) {
envConfig := environmentConfig{}
if _, err := env.UnmarshalFromEnviron(&envConfig); err != nil {
return nil, fmt.Errorf("failed to load config from environment: %w", err)
}
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
var mdnsIface net.Interface
ifaceNames := []string{}
for _, iface := range ifaces {
if iface.Name == envConfig.MDNSInterface {
mdnsIface = iface
}
ifaceNames = append(ifaceNames, iface.Name)
}
if mdnsIface.Name == "" {
return nil, fmt.Errorf("failed to find MDNS interface %s. Found interfaces %s", envConfig.MDNSInterface, strings.Join(ifaceNames, ","))
}
options := mqtt.NewClientOptions().AddBroker(envConfig.MQTTAddress).SetAutoReconnect(true)
var clientIDUnique uint32
if envConfig.PublishTopic != "" {
clientIDUnique = crc32.ChecksumIEEE([]byte(envConfig.PublishTopic))
options.SetBinaryWill(envConfig.PublishTopic, []byte{}, 1, true)
} else {
clientIDUnique = rand.Uint32()
}
options.SetClientID(fmt.Sprintf("mdns2mqtt-%4x", clientIDUnique))
if envConfig.MQTTUsername != "" {
options.SetUsername(envConfig.MQTTUsername)
}
if envConfig.MQTTPassword != "" {
options.SetPassword(envConfig.MQTTPassword)
}
return &Config{
MDNSInterface: &mdnsIface,
MQTTClient: mqtt.NewClient(options),
PublishTopic: envConfig.PublishTopic,
SubscribeTopic: envConfig.SubscribeTopic,
}, nil
}