config
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@ -0,0 +1,2 @@
|
|||||||
|
config.json
|
||||||
|
src/udi/udi
|
||||||
|
35
src/udi/config/config.go
Normal file
35
src/udi/config/config.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import "encoding/json"
|
||||||
|
import "log"
|
||||||
|
import "os"
|
||||||
|
|
||||||
|
|
||||||
|
type ConfigT struct {
|
||||||
|
Mqtt struct {
|
||||||
|
Broker string
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
TlsEnable string
|
||||||
|
}
|
||||||
|
TopicMappings []struct {
|
||||||
|
Topics []string
|
||||||
|
Plugin string
|
||||||
|
}
|
||||||
|
Plugins []struct {
|
||||||
|
Name string
|
||||||
|
DatabaseConnStr string
|
||||||
|
Attributes map[string]interface{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var Config ConfigT
|
||||||
|
|
||||||
|
|
||||||
|
func LoadConfiguration() {
|
||||||
|
err := json.Unmarshal([]byte(os.Getenv("UDI_CONF")), &Config)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Unable to parse configuration: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,7 @@ import "log"
|
|||||||
import "os"
|
import "os"
|
||||||
import "os/signal"
|
import "os/signal"
|
||||||
import um "udi/mqtt"
|
import um "udi/mqtt"
|
||||||
|
import "udi/config"
|
||||||
|
|
||||||
|
|
||||||
func inputDispatcher() {
|
func inputDispatcher() {
|
||||||
@ -21,6 +22,8 @@ func main() {
|
|||||||
|
|
||||||
log.Println("UDI starting")
|
log.Println("UDI starting")
|
||||||
|
|
||||||
|
config.LoadConfiguration()
|
||||||
|
|
||||||
go inputDispatcher()
|
go inputDispatcher()
|
||||||
|
|
||||||
um.StartMqttClient()
|
um.StartMqttClient()
|
||||||
|
@ -8,6 +8,7 @@ import "strings"
|
|||||||
import MQTT "github.com/eclipse/paho.mqtt.golang"
|
import MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||||
import "github.com/google/uuid"
|
import "github.com/google/uuid"
|
||||||
import "crypto/tls"
|
import "crypto/tls"
|
||||||
|
import "udi/config"
|
||||||
|
|
||||||
type Message struct {
|
type Message struct {
|
||||||
Topic string
|
Topic string
|
||||||
@ -71,7 +72,7 @@ func outputDispatcher(client MQTT.Client) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func StartMqttClient() {
|
func StartMqttClient() {
|
||||||
broker := os.Getenv("MQTT_BROKER")
|
broker := config.Config.Mqtt.Broker
|
||||||
if broker == "" {
|
if broker == "" {
|
||||||
log.Fatal("No broker given, set env var MQTT_BROKER")
|
log.Fatal("No broker given, set env var MQTT_BROKER")
|
||||||
}
|
}
|
||||||
@ -88,19 +89,19 @@ func StartMqttClient() {
|
|||||||
SetReconnectingHandler(onReconnecting).
|
SetReconnectingHandler(onReconnecting).
|
||||||
SetConnectRetry(true)
|
SetConnectRetry(true)
|
||||||
|
|
||||||
username := os.Getenv("MQTT_USERNAME")
|
username := config.Config.Mqtt.Username
|
||||||
if username != "" {
|
if username != "" {
|
||||||
opts.SetUsername(username)
|
opts.SetUsername(username)
|
||||||
}
|
}
|
||||||
|
|
||||||
password := os.Getenv("MQTT_PASSWORD")
|
password := config.Config.Mqtt.Password
|
||||||
if password != "" {
|
if password != "" {
|
||||||
opts.SetPassword(password)
|
opts.SetPassword(password)
|
||||||
}
|
}
|
||||||
|
|
||||||
enableTls := os.Getenv("MQTT_ENABLE_TLS")
|
enableTls := config.Config.Mqtt.TlsEnable
|
||||||
if enableTls == "true" {
|
if enableTls == "true" {
|
||||||
log.Println("Enableing TLS connection")
|
log.Println("Enabling TLS connection")
|
||||||
tlsConfig := &tls.Config {
|
tlsConfig := &tls.Config {
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
}
|
}
|
||||||
|
BIN
src/udi/udi
BIN
src/udi/udi
Binary file not shown.
Reference in New Issue
Block a user