Fix topic parsing from env
This commit is contained in:
1 parent
1e1aee1ae1
commit
749ffae0c4
1 file changed
+14
-4
+14
-4
@@ -2,6 +2,7 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
@@ -35,16 +36,25 @@ func NewConfig(logger *zap.Logger) *Config {
|
|||||||
logger.Warn("MQTT_PASSWORD is not set")
|
logger.Warn("MQTT_PASSWORD is not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
topics := os.Getenv("MQTT_TOPICS")
|
topicsEnv := os.Getenv("MQTT_TOPICS")
|
||||||
if topics == "" {
|
var topics []string
|
||||||
|
if topicsEnv == "" {
|
||||||
logger.Warn("MQTT_TOPICS is not set, defaulting to #")
|
logger.Warn("MQTT_TOPICS is not set, defaulting to #")
|
||||||
topics = "#"
|
topics = []string{"#"}
|
||||||
|
} else {
|
||||||
|
parts := strings.Split(topicsEnv, ",")
|
||||||
|
for _, p := range parts {
|
||||||
|
t := strings.TrimSpace(p)
|
||||||
|
if t != "" {
|
||||||
|
topics = append(topics, t)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Config{
|
return &Config{
|
||||||
URI: uri,
|
URI: uri,
|
||||||
Username: username,
|
Username: username,
|
||||||
Password: password,
|
Password: password,
|
||||||
Topics: []string{topics},
|
Topics: topics,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user