Allow setting topic through URL query parameters
Closes: https://codeberg.org/xenrox/ntfy-alertmanager/issues/2
This commit is contained in:
parent
84a45a909b
commit
bd1abedac1
2 changed files with 13 additions and 4 deletions
|
@ -69,6 +69,8 @@ ntfy {
|
|||
server https://ntfy.sh
|
||||
# Name of the ntfy topic. For backwards compatibility you can specify the full URL of the
|
||||
# topic (e.g. https://ntfy.sh/alertmanager-alerts) and the server will be parsed from it.
|
||||
# Furthermore the topic name can be optionally set by using URL parameters with the webhook
|
||||
# endpoint: https://ntfy-alertmanager.example.com/?topic=foobar
|
||||
# This setting is required.
|
||||
# Default: ""
|
||||
topic alertmanager-alerts
|
||||
|
|
15
main.go
15
main.go
|
@ -304,8 +304,13 @@ func (br *bridge) multiAlertNotification(p *payload) *notification {
|
|||
return n
|
||||
}
|
||||
|
||||
func (br *bridge) publish(n *notification) error {
|
||||
url, err := br.topicURL(n.topic)
|
||||
func (br *bridge) publish(n *notification, topicParam string) error {
|
||||
// precedence: topicParam > n.topic > cfg.Ntfy.Topic
|
||||
if topicParam == "" {
|
||||
topicParam = n.topic
|
||||
}
|
||||
|
||||
url, err := br.topicURL(topicParam)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -437,6 +442,8 @@ func (br *bridge) handleWebhooks(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
topicParam := r.URL.Query().Get("topic")
|
||||
|
||||
var event payload
|
||||
if err := json.NewDecoder(r.Body).Decode(&event); err != nil {
|
||||
http.Error(w, "Failed to parse payload", http.StatusInternalServerError)
|
||||
|
@ -451,7 +458,7 @@ func (br *bridge) handleWebhooks(w http.ResponseWriter, r *http.Request) {
|
|||
if br.cfg.AlertMode == config.Single {
|
||||
notifications := br.singleAlertNotifications(&event)
|
||||
for _, n := range notifications {
|
||||
err := br.publish(n)
|
||||
err := br.publish(n, topicParam)
|
||||
if err != nil {
|
||||
logger.Error("Failed to publish notification",
|
||||
slog.String("error", err.Error()))
|
||||
|
@ -465,7 +472,7 @@ func (br *bridge) handleWebhooks(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
} else {
|
||||
notification := br.multiAlertNotification(&event)
|
||||
err := br.publish(notification)
|
||||
err := br.publish(notification, topicParam)
|
||||
if err != nil {
|
||||
logger.Error("Failed to publish notification",
|
||||
slog.String("error", err.Error()))
|
||||
|
|
Loading…
Add table
Reference in a new issue