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/httprate v0.9.0
|
||||||
github.com/go-chi/jwtauth/v5 v5.3.1
|
github.com/go-chi/jwtauth/v5 v5.3.1
|
||||||
github.com/go-chi/render v1.0.3
|
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/golang-migrate/migrate/v4 v4.17.1
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/gorilla/websocket v1.5.3
|
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/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 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4=
|
||||||
github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0=
|
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 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
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=
|
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||||
|
|
|
@ -70,9 +70,12 @@ type NotifyService struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *NotifyService) GetS(k, defaultVal string) string {
|
func (n *NotifyService) GetS(k, defaultVal string) string {
|
||||||
if v, has := n.Config[k].(string); has {
|
if v, has := n.Config[k]; has {
|
||||||
|
if v, isString := v.(string); isString {
|
||||||
return v
|
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
|
return defaultVal
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
|
|
||||||
"dynatron.me/x/stillbox/pkg/config"
|
"dynatron.me/x/stillbox/pkg/config"
|
||||||
|
|
||||||
|
"github.com/go-viper/mapstructure/v2"
|
||||||
"github.com/nikoksr/notify"
|
"github.com/nikoksr/notify"
|
||||||
"github.com/nikoksr/notify/service/http"
|
"github.com/nikoksr/notify/service/http"
|
||||||
)
|
)
|
||||||
|
@ -19,9 +20,7 @@ type notifier struct {
|
||||||
*notify.Notify
|
*notify.Notify
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *notifier) buildSlackWebhookPayload(cfg config.NotifyService) func(string, string) any {
|
func (n *notifier) buildSlackWebhookPayload(cfg *slackWebhookConfig) func(string, string) any {
|
||||||
icon := cfg.GetS("icon", "🚨")
|
|
||||||
url := cfg.GetS("messageURL", "")
|
|
||||||
|
|
||||||
type Attachment struct {
|
type Attachment struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
|
@ -42,27 +41,40 @@ func (n *notifier) buildSlackWebhookPayload(cfg config.NotifyService) func(strin
|
||||||
{
|
{
|
||||||
Title: subject,
|
Title: subject,
|
||||||
Text: message,
|
Text: message,
|
||||||
TitleLink: url,
|
TitleLink: cfg.MessageURL,
|
||||||
Timestamp: time.Now().Unix(),
|
Timestamp: time.Now().Unix(),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
IconEmoji: icon,
|
IconEmoji: cfg.Icon,
|
||||||
}
|
}
|
||||||
|
|
||||||
return m
|
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 {
|
func (n *notifier) addService(cfg config.NotifyService) error {
|
||||||
switch cfg.Provider {
|
switch cfg.Provider {
|
||||||
case "slackwebhook":
|
case "slackwebhook":
|
||||||
|
swc := &slackWebhookConfig{
|
||||||
|
Icon: "🚨",
|
||||||
|
}
|
||||||
|
err := mapstructure.Decode(cfg.Config, &swc)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
hs := http.New()
|
hs := http.New()
|
||||||
hs.AddReceivers(&http.Webhook{
|
hs.AddReceivers(&http.Webhook{
|
||||||
ContentType: "application/json",
|
ContentType: "application/json",
|
||||||
Header: make(stdhttp.Header),
|
Header: make(stdhttp.Header),
|
||||||
Method: stdhttp.MethodPost,
|
Method: stdhttp.MethodPost,
|
||||||
URL: cfg.GetS("webhookURL", ""),
|
URL: swc.WebhookURL,
|
||||||
BuildPayload: n.buildSlackWebhookPayload(cfg),
|
BuildPayload: n.buildSlackWebhookPayload(swc),
|
||||||
})
|
})
|
||||||
n.UseServices(hs)
|
n.UseServices(hs)
|
||||||
default:
|
default:
|
||||||
|
|
Loading…
Reference in a new issue