Use mapstructure for notify config
This commit is contained in:
parent
b40144447f
commit
10e4eff17a
4 changed files with 27 additions and 9 deletions
1
go.mod
1
go.mod
|
@ -11,6 +11,7 @@ require (
|
|||
github.com/go-chi/httprate v0.9.0
|
||||
github.com/go-chi/jwtauth/v5 v5.3.1
|
||||
github.com/go-chi/render v1.0.3
|
||||
github.com/go-viper/mapstructure/v2 v2.2.1
|
||||
github.com/golang-migrate/migrate/v4 v4.17.1
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
|
|
2
go.sum
2
go.sum
|
@ -44,6 +44,8 @@ github.com/go-chi/jwtauth/v5 v5.3.1 h1:1ePWrjVctvp1tyBq5b/2ER8Th/+RbYc7x4qNsc5rh
|
|||
github.com/go-chi/jwtauth/v5 v5.3.1/go.mod h1:6Fl2RRmWXs3tJYE1IQGX81FsPoGqDwq9c15j52R5q80=
|
||||
github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4=
|
||||
github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0=
|
||||
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
|
||||
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
|
|
|
@ -70,8 +70,11 @@ type NotifyService struct {
|
|||
}
|
||||
|
||||
func (n *NotifyService) GetS(k, defaultVal string) string {
|
||||
if v, has := n.Config[k].(string); has {
|
||||
return v
|
||||
if v, has := n.Config[k]; has {
|
||||
if v, isString := v.(string); isString {
|
||||
return v
|
||||
}
|
||||
log.Error().Str("configKey", k).Str("provider", n.Provider).Str("default", defaultVal).Msg("notify config value is not a string! using default")
|
||||
}
|
||||
|
||||
return defaultVal
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"dynatron.me/x/stillbox/pkg/config"
|
||||
|
||||
"github.com/go-viper/mapstructure/v2"
|
||||
"github.com/nikoksr/notify"
|
||||
"github.com/nikoksr/notify/service/http"
|
||||
)
|
||||
|
@ -19,9 +20,7 @@ type notifier struct {
|
|||
*notify.Notify
|
||||
}
|
||||
|
||||
func (n *notifier) buildSlackWebhookPayload(cfg config.NotifyService) func(string, string) any {
|
||||
icon := cfg.GetS("icon", "🚨")
|
||||
url := cfg.GetS("messageURL", "")
|
||||
func (n *notifier) buildSlackWebhookPayload(cfg *slackWebhookConfig) func(string, string) any {
|
||||
|
||||
type Attachment struct {
|
||||
Title string `json:"title"`
|
||||
|
@ -42,27 +41,40 @@ func (n *notifier) buildSlackWebhookPayload(cfg config.NotifyService) func(strin
|
|||
{
|
||||
Title: subject,
|
||||
Text: message,
|
||||
TitleLink: url,
|
||||
TitleLink: cfg.MessageURL,
|
||||
Timestamp: time.Now().Unix(),
|
||||
},
|
||||
},
|
||||
IconEmoji: icon,
|
||||
IconEmoji: cfg.Icon,
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
}
|
||||
|
||||
type slackWebhookConfig struct {
|
||||
WebhookURL string `mapstructure:"webhookURL"`
|
||||
Icon string `mapstructure:"icon"`
|
||||
MessageURL string `mapstructure:"messageURL"`
|
||||
}
|
||||
|
||||
func (n *notifier) addService(cfg config.NotifyService) error {
|
||||
switch cfg.Provider {
|
||||
case "slackwebhook":
|
||||
swc := &slackWebhookConfig{
|
||||
Icon: "🚨",
|
||||
}
|
||||
err := mapstructure.Decode(cfg.Config, &swc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hs := http.New()
|
||||
hs.AddReceivers(&http.Webhook{
|
||||
ContentType: "application/json",
|
||||
Header: make(stdhttp.Header),
|
||||
Method: stdhttp.MethodPost,
|
||||
URL: cfg.GetS("webhookURL", ""),
|
||||
BuildPayload: n.buildSlackWebhookPayload(cfg),
|
||||
URL: swc.WebhookURL,
|
||||
BuildPayload: n.buildSlackWebhookPayload(swc),
|
||||
})
|
||||
n.UseServices(hs)
|
||||
default:
|
||||
|
|
Loading…
Reference in a new issue