Review user-agent string for some notifications

This commit is contained in:
CrazyMax
2020-02-17 22:53:15 +01:00
parent f8d88e3893
commit 0710c4cef9
4 changed files with 24 additions and 18 deletions

View File

@@ -42,8 +42,11 @@ func New(cfg *config.Config, location *time.Location) (*Diun, error) {
return nil, err return nil, err
} }
// User-Agent
userAgent := fmt.Sprintf("diun/%s go/%s %s", cfg.App.Version, runtime.Version()[2:], strings.Title(runtime.GOOS))
// Notification client // Notification client
notifcli, err := notif.New(cfg.Notif, cfg.App) notifcli, err := notif.New(cfg.Notif, cfg.App, userAgent)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -55,7 +58,7 @@ func New(cfg *config.Config, location *time.Location) (*Diun, error) {
)), )),
db: dbcli, db: dbcli,
notif: notifcli, notif: notifcli,
userAgent: fmt.Sprintf("diun/%s go/%s %s", cfg.App.Version, runtime.Version()[2:], strings.Title(runtime.GOOS)), userAgent: userAgent,
}, nil }, nil
} }

View File

@@ -19,7 +19,7 @@ type Client struct {
} }
// New creates a new notification instance // New creates a new notification instance
func New(config model.Notif, app model.App) (*Client, error) { func New(config model.Notif, app model.App, userAgent string) (*Client, error) {
var c = &Client{ var c = &Client{
cfg: config, cfg: config,
app: app, app: app,
@@ -37,10 +37,10 @@ func New(config model.Notif, app model.App) (*Client, error) {
c.notifiers = append(c.notifiers, telegram.New(config.Telegram, app)) c.notifiers = append(c.notifiers, telegram.New(config.Telegram, app))
} }
if config.Webhook.Enable { if config.Webhook.Enable {
c.notifiers = append(c.notifiers, webhook.New(config.Webhook, app)) c.notifiers = append(c.notifiers, webhook.New(config.Webhook, app, userAgent))
} }
if config.Gotify.Enable { if config.Gotify.Enable {
c.notifiers = append(c.notifiers, gotify.New(config.Gotify, app)) c.notifiers = append(c.notifiers, gotify.New(config.Gotify, app, userAgent))
} }
log.Debug().Msgf("%d notifier(s) created", len(c.notifiers)) log.Debug().Msgf("%d notifier(s) created", len(c.notifiers))

View File

@@ -21,14 +21,16 @@ type Client struct {
*notifier.Notifier *notifier.Notifier
cfg model.NotifGotify cfg model.NotifGotify
app model.App app model.App
userAgent string
} }
// New creates a new gotify notification instance // New creates a new gotify notification instance
func New(config model.NotifGotify, app model.App) notifier.Notifier { func New(config model.NotifGotify, app model.App, userAgent string) notifier.Notifier {
return notifier.Notifier{ return notifier.Notifier{
Handler: &Client{ Handler: &Client{
cfg: config, cfg: config,
app: app, app: app,
userAgent: userAgent,
}, },
} }
} }
@@ -77,7 +79,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode()))) req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
req.Header.Set("User-Agent", fmt.Sprintf("%s %s", c.app.Name, c.app.Version)) req.Header.Set("User-Agent", c.userAgent)
resp, err := hc.Do(req) resp, err := hc.Do(req)
if err != nil { if err != nil {

View File

@@ -3,7 +3,6 @@ package webhook
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"time" "time"
@@ -17,14 +16,16 @@ type Client struct {
*notifier.Notifier *notifier.Notifier
cfg model.NotifWebhook cfg model.NotifWebhook
app model.App app model.App
userAgent string
} }
// New creates a new webhook notification instance // New creates a new webhook notification instance
func New(config model.NotifWebhook, app model.App) notifier.Notifier { func New(config model.NotifWebhook, app model.App, userAgent string) notifier.Notifier {
return notifier.Notifier{ return notifier.Notifier{
Handler: &Client{ Handler: &Client{
cfg: config, cfg: config,
app: app, app: app,
userAgent: userAgent,
}, },
} }
} }
@@ -76,7 +77,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
} }
} }
req.Header.Set("User-Agent", fmt.Sprintf("%s %s", c.app.Name, c.app.Version)) req.Header.Set("User-Agent", c.userAgent)
_, err = hc.Do(req) _, err = hc.Do(req)
return err return err