diff --git a/config.scfg b/config.scfg index a23917d..4763a63 100644 --- a/config.scfg +++ b/config.scfg @@ -51,7 +51,7 @@ labels { } instance "example.com" { - topic https://ntfy.sh/homeserver + topic homeserver tags "computer,example" } } @@ -64,10 +64,14 @@ resolved { } ntfy { - # URL of the ntfy topic. + # URL of the ntfy server. + # Default: "https://ntfy.sh" + 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. # This setting is required. # Default: "" - topic https://ntfy.sh/alertmanager-alerts + topic alertmanager-alerts # ntfy authentication via Basic Auth (https://docs.ntfy.sh/publish/#username-password) # Default: "" user user diff --git a/config/config.go b/config/config.go index 4cd40de..ce6ed8e 100644 --- a/config/config.go +++ b/config/config.go @@ -37,6 +37,7 @@ type Config struct { } type ntfyConfig struct { + Server string Topic string User string Password string @@ -230,6 +231,13 @@ func parseBlock(block scfg.Block, config *Config) error { ntfyDir := block.Get("ntfy") if ntfyDir != nil { + d = ntfyDir.Children.Get("server") + if d != nil { + if err := d.ParseParams(&config.Ntfy.Server); err != nil { + return err + } + } + d = ntfyDir.Children.Get("topic") if d != nil { if err := d.ParseParams(&config.Ntfy.Topic); err != nil { @@ -422,6 +430,8 @@ func Read(path string) (*Config, error) { config.LogFormat = "text" config.AlertMode = Multi + config.Ntfy.Server = "https://ntfy.sh" + config.Cache.Type = "disabled" config.Cache.Duration = time.Hour * 24 // memory diff --git a/config/config_test.go b/config/config_test.go index 34c255d..872053d 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -36,7 +36,7 @@ labels { instance "example.com" { tags "computer,example" - topic https://ntfy.sh/homeserver + topic homeserver } } @@ -78,6 +78,7 @@ cache { User: "webhookUser", Password: "webhookPass", Ntfy: ntfyConfig{ + Server: "https://ntfy.sh", Topic: "https://ntfy.sh/alertmanager-alerts", User: "user", Password: "pass", @@ -96,7 +97,7 @@ cache { "severity:info": {Priority: "1"}, "instance:example.com": { Tags: []string{"computer", "example"}, - Topic: "https://ntfy.sh/homeserver"}, + Topic: "homeserver"}, }, }, Cache: CacheConfig{ diff --git a/main.go b/main.go index 8003338..b1780f1 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ import ( "io" "log/slog" "net/http" + "net/url" "os" "os/signal" "slices" @@ -304,9 +305,9 @@ func (br *bridge) multiAlertNotification(p *payload) *notification { } func (br *bridge) publish(n *notification) error { - url := br.cfg.Ntfy.Topic - if n.topic != "" { - url = n.topic + url, err := br.topicURL(n.topic) + if err != nil { + return err } req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(n.body)) @@ -616,3 +617,22 @@ func main() { slog.String("error", err.Error())) } } + +func (br *bridge) topicURL(topic string) (string, error) { + if topic == "" { + topic = br.cfg.Ntfy.Topic + } + + // Check if the configured topic name already contains the ntfy server + i := strings.Index(topic, "://") + if i != -1 { + return topic, nil + } + + s, err := url.JoinPath(br.cfg.Ntfy.Server, topic) + if err != nil { + return "", err + } + + return s, nil +}