Fix topic parsing from env
This commit is contained in:
		@@ -2,6 +2,7 @@ package config
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"os"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/joho/godotenv"
 | 
			
		||||
	"go.uber.org/zap"
 | 
			
		||||
@@ -35,16 +36,25 @@ func NewConfig(logger *zap.Logger) *Config {
 | 
			
		||||
		logger.Warn("MQTT_PASSWORD is not set")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	topics := os.Getenv("MQTT_TOPICS")
 | 
			
		||||
	if topics == "" {
 | 
			
		||||
	topicsEnv := os.Getenv("MQTT_TOPICS")
 | 
			
		||||
	var topics []string
 | 
			
		||||
	if topicsEnv == "" {
 | 
			
		||||
		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{
 | 
			
		||||
		URI:      uri,
 | 
			
		||||
		Username: username,
 | 
			
		||||
		Password: password,
 | 
			
		||||
		Topics:   []string{topics},
 | 
			
		||||
		Topics:   topics,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user