Allow to configure scheme for MQTT broker (#292)

This commit is contained in:
Francois Blackburn
2021-03-07 11:05:11 -05:00
committed by GitHub
parent 456b1f5e5c
commit b912aee5e4
6 changed files with 9 additions and 1 deletions

View File

@@ -8,6 +8,7 @@ You can send notifications to any MQTT compatible server with the following sett
```yaml ```yaml
notif: notif:
mqtt: mqtt:
scheme: mqtt
host: localhost host: localhost
port: 1883 port: 1883
username: guest username: guest
@@ -19,6 +20,7 @@ You can send notifications to any MQTT compatible server with the following sett
| Name | Default | Description | | Name | Default | Description |
|--------------------|---------------|---------------| |--------------------|---------------|---------------|
| `scheme`[^1] | `mqtt` | MQTT server scheme (`mqtt`, `mqtts`, `ws` or `wss`) |
| `host`[^1] | `localhost` | MQTT server host | | `host`[^1] | `localhost` | MQTT server host |
| `port`[^1] | `1883` | MQTT server port | | `port`[^1] | `1883` | MQTT server port |
| `username` | | MQTT username | | `username` | | MQTT username |
@@ -30,6 +32,7 @@ You can send notifications to any MQTT compatible server with the following sett
| `qos` | `0` | Ensured message delivery at specified Quality of Service (QoS) | | `qos` | `0` | Ensured message delivery at specified Quality of Service (QoS) |
!!! abstract "Environment variables" !!! abstract "Environment variables"
* `DIUN_NOTIF_MQTT_SCHEME`
* `DIUN_NOTIF_MQTT_HOST` * `DIUN_NOTIF_MQTT_HOST`
* `DIUN_NOTIF_MQTT_PORT` * `DIUN_NOTIF_MQTT_PORT`
* `DIUN_NOTIF_MQTT_USERNAME` * `DIUN_NOTIF_MQTT_USERNAME`

View File

@@ -123,6 +123,7 @@ func TestLoadFile(t *testing.T) {
MsgType: model.NotifMatrixMsgTypeNotice, MsgType: model.NotifMatrixMsgTypeNotice,
}, },
Mqtt: &model.NotifMqtt{ Mqtt: &model.NotifMqtt{
Scheme: "mqtt",
Host: "localhost", Host: "localhost",
Port: 1883, Port: 1883,
Username: "guest", Username: "guest",

View File

@@ -44,6 +44,7 @@ notif:
password: bar password: bar
roomID: "!abcdefGHIjklmno:matrix.org" roomID: "!abcdefGHIjklmno:matrix.org"
mqtt: mqtt:
scheme: "mqtt"
host: "localhost" host: "localhost"
port: 1883 port: 1883
username: "guest" username: "guest"

View File

@@ -44,6 +44,7 @@ notif:
password: bar password: bar
roomID: "!abcdefGHIjklmno:matrix.org" roomID: "!abcdefGHIjklmno:matrix.org"
mqtt: mqtt:
scheme: "mqtt"
host: "localhost" host: "localhost"
port: 1883 port: 1883
username: "guest" username: "guest"

View File

@@ -1,6 +1,7 @@
package model package model
type NotifMqtt struct { type NotifMqtt struct {
Scheme string `yaml:"scheme,omitempty" json:"scheme,omitempty" validate:"required,oneof=mqtt mqtts ws wss"`
Host string `yaml:"host,omitempty" json:"host,omitempty" validate:"required"` Host string `yaml:"host,omitempty" json:"host,omitempty" validate:"required"`
Port int `yaml:"port,omitempty" json:"port,omitempty" validate:"required,min=1"` Port int `yaml:"port,omitempty" json:"port,omitempty" validate:"required,min=1"`
Username string `yaml:"username,omitempty" json:"username,omitempty" validate:"omitempty"` Username string `yaml:"username,omitempty" json:"username,omitempty" validate:"omitempty"`
@@ -21,6 +22,7 @@ func (s *NotifMqtt) GetDefaults() *NotifMqtt {
// SetDefaults sets the default values // SetDefaults sets the default values
func (s *NotifMqtt) SetDefaults() { func (s *NotifMqtt) SetDefaults() {
s.Scheme = "mqtt"
s.Host = "localhost" s.Host = "localhost"
s.Port = 1883 s.Port = 1883
s.QoS = 0 s.QoS = 0

View File

@@ -49,7 +49,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
return err return err
} }
broker := fmt.Sprintf("tcp://%s:%d", c.cfg.Host, c.cfg.Port) broker := fmt.Sprintf("%s://%s:%d", c.cfg.Scheme, c.cfg.Host, c.cfg.Port)
opts := MQTT.NewClientOptions().AddBroker(broker).SetClientID(c.cfg.Client) opts := MQTT.NewClientOptions().AddBroker(broker).SetClientID(c.cfg.Client)
opts.Username = username opts.Username = username
opts.Password = password opts.Password = password