From 4550b525fad60166fff16f1e090d905b6e9086e4 Mon Sep 17 00:00:00 2001 From: Mika Hiltunen Date: Sun, 14 Apr 2024 13:15:19 +0300 Subject: [PATCH] #6: Fix Gotify URL handling --- config/config.go | 11 +++++++++-- notifications/gotify.go | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index 0175666..4efc79c 100644 --- a/config/config.go +++ b/config/config.go @@ -46,9 +46,16 @@ func LoadConfig() (*AppConfig, error) { appConfig.AppVersion = appVersion - // Validate base URL + if appConfig.GotifyEnabled && (appConfig.GotifyToken == "" || appConfig.GotifyURL == "") { + return nil, fmt.Errorf("gotify notifications are enabled but token or gotify URL is empty") + } + + // Validate trailing slash of URLS if !strings.HasSuffix(appConfig.BaseURL, "/") { - return nil, fmt.Errorf("base URL must have a trailing slash") + appConfig.BaseURL = fmt.Sprintf("%s/", appConfig.BaseURL) + } + if appConfig.GotifyURL != "" && !strings.HasSuffix(appConfig.GotifyURL, "/") { + appConfig.GotifyURL = fmt.Sprintf("%s/", appConfig.GotifyURL) } return appConfig, nil diff --git a/notifications/gotify.go b/notifications/gotify.go index e05471f..80cd796 100644 --- a/notifications/gotify.go +++ b/notifications/gotify.go @@ -52,7 +52,7 @@ func (n *Notifier) SendGotifyMessage(title string, message string) error { return fmt.Errorf("marshaling request body failed: %w", err) } - gotifyUrl := fmt.Sprintf("%s/message", n.appConfig.GotifyURL) + gotifyUrl := fmt.Sprintf("%smessage", n.appConfig.GotifyURL) req, err := http.NewRequest(http.MethodPost, gotifyUrl, bytes.NewBuffer(data)) if err != nil { return fmt.Errorf("creating a request failed: %w", err)