Pass parameters directly instead of through a config struct

This commit is contained in:
2022-11-18 20:09:52 +01:00
parent 2df59cdb17
commit 6368fce40d
9 changed files with 53 additions and 76 deletions

View File

@@ -1,10 +0,0 @@
package mqtt
type Config struct {
Host string `yaml:"host" envconfig:"MQTT_HOST"`
Port string `yaml:"port" envconfig:"MQTT_PORT"`
Username string `yaml:"username" envconfig:"MQTT_USERNAME"`
Password string `yaml:"password" envconfig:"MQTT_PASSWORD"`
ClientID string `yaml:"client_id" envconfig:"MQTT_CLIENT_ID"`
}

View File

@@ -12,12 +12,12 @@ var defaultHandler paho.MessageHandler = func(client paho.Client, msg paho.Messa
fmt.Printf("MSG: %s\n", msg.Payload())
}
func New(config Config) paho.Client {
opts := paho.NewClientOptions().AddBroker(fmt.Sprintf("%s:%s", config.Host, config.Port))
opts.SetClientID(config.ClientID)
func New(host string, port int, clientID string, username string, password string) paho.Client {
opts := paho.NewClientOptions().AddBroker(fmt.Sprintf("%s:%d", host, port))
opts.SetClientID(clientID)
opts.SetDefaultPublishHandler(defaultHandler)
opts.SetUsername(config.Username)
opts.SetPassword(config.Password)
opts.SetUsername(username)
opts.SetPassword(password)
opts.SetOrderMatters(false)
client := paho.NewClient(opts)