mirror of
https://github.com/crazy-max/diun.git
synced 2025-12-24 14:31:47 +01:00
Fix doc and coding style for MQTT notifier
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# Mqtt notifications
|
||||
# MQTT notifications
|
||||
|
||||
You can send notifications to any mqtt compatible server with the following settings.
|
||||
You can send notifications to any MQTT compatible server with the following settings.
|
||||
|
||||
## Configuration
|
||||
|
||||
@@ -12,8 +12,8 @@ You can send notifications to any mqtt compatible server with the following sett
|
||||
port: 1883
|
||||
username: guest
|
||||
password: guest
|
||||
topic: docker/diun
|
||||
client: diun
|
||||
topic: docker/diun
|
||||
qos: 0
|
||||
```
|
||||
|
||||
@@ -21,13 +21,13 @@ You can send notifications to any mqtt compatible server with the following sett
|
||||
|--------------------|---------------|---------------|
|
||||
| `host`[^1] | `localhost` | MQTT server host |
|
||||
| `port`[^1] | `1883` | MQTT server port |
|
||||
| `client`[^1] | `diun-client` | Name of the client which connects to the server |
|
||||
| `topic`[^1] | `docker/diun` | Topic the message will be sent to |
|
||||
| `username` | | MQTT username |
|
||||
| `usernameFile` | | Use content of secret file as MQTT username if `username` not defined |
|
||||
| `password` | | MQTT password |
|
||||
| `passwordFile` | | Use content of secret file as MQTT password if `password` not defined |
|
||||
| `qos` | `0` | Topic the message will be sent to |
|
||||
| `client`[^1] | | Client id to be used by this client when connecting to the MQTT broker |
|
||||
| `topic`[^1] | | Topic the message will be sent to |
|
||||
| `qos` | `0` | Ensured message delivery at specified Quality of Service (QoS) |
|
||||
|
||||
## Sample
|
||||
|
||||
|
||||
@@ -121,6 +121,15 @@ func TestLoadFile(t *testing.T) {
|
||||
RoomID: "!abcdefGHIjklmno:matrix.org",
|
||||
MsgType: model.NotifMatrixMsgTypeNotice,
|
||||
},
|
||||
Mqtt: &model.NotifMqtt{
|
||||
Host: "localhost",
|
||||
Port: 1883,
|
||||
Username: "guest",
|
||||
Password: "guest",
|
||||
Client: "diun",
|
||||
Topic: "docker/diun",
|
||||
QoS: 0,
|
||||
},
|
||||
RocketChat: &model.NotifRocketChat{
|
||||
Endpoint: "http://rocket.foo.com:3000",
|
||||
Channel: "#general",
|
||||
|
||||
@@ -42,6 +42,14 @@ notif:
|
||||
user: "@foo:matrix.org"
|
||||
password: bar
|
||||
roomID: "!abcdefGHIjklmno:matrix.org"
|
||||
mqtt:
|
||||
host: "localhost"
|
||||
port: 1883
|
||||
username: "guest"
|
||||
password: "guest"
|
||||
client: "diun"
|
||||
topic: "docker/diun"
|
||||
qos: 0
|
||||
rocketchat:
|
||||
endpoint: http://rocket.foo.com:3000
|
||||
channel: "#general"
|
||||
|
||||
@@ -42,6 +42,14 @@ notif:
|
||||
user: "@foo:matrix.org"
|
||||
password: bar
|
||||
roomID: "!abcdefGHIjklmno:matrix.org"
|
||||
mqtt:
|
||||
host: "localhost"
|
||||
port: 1883
|
||||
username: "guest"
|
||||
password: "guest"
|
||||
client: "diun"
|
||||
topic: "docker/diun"
|
||||
qos: 0
|
||||
rocketchat:
|
||||
endpoint: http://rocket.foo.com:3000
|
||||
channel: "#general"
|
||||
|
||||
@@ -29,7 +29,7 @@ type Notif struct {
|
||||
Gotify *NotifGotify `yaml:"gotify,omitempty" json:"gotify,omitempty"`
|
||||
Mail *NotifMail `yaml:"mail,omitempty" json:"mail,omitempty"`
|
||||
Matrix *NotifMatrix `yaml:"matrix,omitempty" json:"matrix,omitempty"`
|
||||
Mqtt *NotifMqtt `yaml:"mqtt,omitempty" json:"mqtt,omitempty"`
|
||||
Mqtt *NotifMqtt `yaml:"mqtt,omitempty" json:"mqtt,omitempty"`
|
||||
RocketChat *NotifRocketChat `yaml:"rocketchat,omitempty" json:"rocketchat,omitempty"`
|
||||
Script *NotifScript `yaml:"script,omitempty" json:"script,omitempty"`
|
||||
Slack *NotifSlack `yaml:"slack,omitempty" json:"slack,omitempty"`
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package model
|
||||
|
||||
type NotifMqtt struct {
|
||||
Host string `yaml:"host,omitempty" json:"host,omitempty" validate:"required"`
|
||||
Port int `yaml:"port,omitempty" json:"port,omitempty" validate:"required,min=1"`
|
||||
Username string `yaml:"username,omitempty" json:"username,omitempty" validate:"omitempty"`
|
||||
UsernameFile string `yaml:"usernameFile,omitempty" json:"usernameFile,omitempty" validate:"omitempty,file"`
|
||||
Password string `yaml:"password,omitempty" json:"password,omitempty" validate:"omitempty"`
|
||||
PasswordFile string `yaml:"passwordFile,omitempty" json:"passwordFile,omitempty" validate:"omitempty,file"`
|
||||
Host string `yaml:"host,omitempty" json:"host,omitempty" validate:"required"`
|
||||
Port int `yaml:"port,omitempty" json:"port,omitempty" validate:"required,min=1"`
|
||||
Topic string `yaml:"topic,omitempty" json:"topic,omitempty" validate:"required"`
|
||||
Client string `yaml:"client,omitempty" json:"client,omitempty" validate:"required"`
|
||||
Topic string `yaml:"topic,omitempty" json:"topic,omitempty" validate:"required"`
|
||||
QoS int `yaml:"qos,omitempty" json:"qos,omitempty" validate:"omitempty"`
|
||||
}
|
||||
|
||||
@@ -23,7 +23,5 @@ func (s *NotifMqtt) GetDefaults() *NotifMqtt {
|
||||
func (s *NotifMqtt) SetDefaults() {
|
||||
s.Host = "localhost"
|
||||
s.Port = 1883
|
||||
s.Topic = "docker/diun"
|
||||
s.Client = "diun-client"
|
||||
s.QoS = 0
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package notif
|
||||
|
||||
import (
|
||||
"github.com/crazy-max/diun/v4/internal/notif/mqtt"
|
||||
"strings"
|
||||
|
||||
"github.com/crazy-max/diun/v4/internal/model"
|
||||
@@ -10,6 +9,7 @@ import (
|
||||
"github.com/crazy-max/diun/v4/internal/notif/gotify"
|
||||
"github.com/crazy-max/diun/v4/internal/notif/mail"
|
||||
"github.com/crazy-max/diun/v4/internal/notif/matrix"
|
||||
"github.com/crazy-max/diun/v4/internal/notif/mqtt"
|
||||
"github.com/crazy-max/diun/v4/internal/notif/notifier"
|
||||
"github.com/crazy-max/diun/v4/internal/notif/rocketchat"
|
||||
"github.com/crazy-max/diun/v4/internal/notif/script"
|
||||
|
||||
@@ -3,11 +3,11 @@ package mqtt
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/crazy-max/diun/v4/pkg/utl"
|
||||
"time"
|
||||
|
||||
"github.com/crazy-max/diun/v4/internal/model"
|
||||
"github.com/crazy-max/diun/v4/internal/notif/notifier"
|
||||
"github.com/crazy-max/diun/v4/pkg/utl"
|
||||
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
@@ -16,8 +16,8 @@ import (
|
||||
// Client represents an active mqtt notification object
|
||||
type Client struct {
|
||||
*notifier.Notifier
|
||||
cfg *model.NotifMqtt
|
||||
meta model.Meta
|
||||
cfg *model.NotifMqtt
|
||||
meta model.Meta
|
||||
logger zerolog.Logger
|
||||
}
|
||||
|
||||
@@ -25,8 +25,8 @@ type Client struct {
|
||||
func New(config *model.NotifMqtt, meta model.Meta) notifier.Notifier {
|
||||
return notifier.Notifier{
|
||||
Handler: &Client{
|
||||
cfg: config,
|
||||
meta: meta,
|
||||
cfg: config,
|
||||
meta: meta,
|
||||
logger: log.With().Str("notif", "mqtt").Logger(),
|
||||
},
|
||||
}
|
||||
@@ -59,8 +59,6 @@ func (c *Client) Send(entry model.NotifEntry) error {
|
||||
return token.Error()
|
||||
}
|
||||
|
||||
log.Debug().Msgf("Connected to broker: %s", broker)
|
||||
|
||||
message, err := json.Marshal(struct {
|
||||
Version string `json:"diun_version"`
|
||||
Hostname string `json:"hostname"`
|
||||
@@ -86,9 +84,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Debug().Msgf("Publishing to topic: %s", c.cfg.Topic)
|
||||
token := client.Publish(c.cfg.Topic, byte(c.cfg.QoS), false, message)
|
||||
token.Wait()
|
||||
|
||||
return token.Error()
|
||||
}
|
||||
|
||||
@@ -94,6 +94,7 @@ nav:
|
||||
- Gotify: notif/gotify.md
|
||||
- Mail: notif/mail.md
|
||||
- Matrix: notif/matrix.md
|
||||
- MQTT: notif/mqtt.md
|
||||
- Rocket.Chat: notif/rocketchat.md
|
||||
- Script: notif/script.md
|
||||
- Slack: notif/slack.md
|
||||
|
||||
Reference in New Issue
Block a user