From 89f0afe334cd37b47b22637d7cb8332dc76526e4 Mon Sep 17 00:00:00 2001 From: Jose Henrique Date: Sat, 16 Aug 2025 22:46:02 -0300 Subject: [PATCH] improving logging --- haven-notify/main.go | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/haven-notify/main.go b/haven-notify/main.go index 29339b0..ecdf597 100644 --- a/haven-notify/main.go +++ b/haven-notify/main.go @@ -23,7 +23,9 @@ func main() { } func notifyHandler(w http.ResponseWriter, r *http.Request) { + log.Printf("Incoming %s request from %s to %s", r.Method, r.RemoteAddr, r.URL.Path) if r.Method != http.MethodPost { + log.Printf("Method not allowed: %s", r.Method) w.WriteHeader(http.StatusMethodNotAllowed) w.Write([]byte("Method not allowed")) return @@ -31,31 +33,36 @@ func notifyHandler(w http.ResponseWriter, r *http.Request) { var notif Notification if err := json.NewDecoder(r.Body).Decode(¬if); err != nil { + log.Printf("Invalid payload: %v", err) w.WriteHeader(http.StatusBadRequest) w.Write([]byte("Invalid payload")) return } + log.Printf("Received notification payload: Title='%s', Message='%s'", notif.Title, notif.Message) // Call Discord notification function if err := sendDiscordNotification(notif.Title, notif.Message); err != nil { + log.Printf("Failed to send Discord notification: %v", err) w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("Failed to send Discord notification")) return } + log.Printf("Notification sent successfully for Title='%s'", notif.Title) w.WriteHeader(http.StatusOK) w.Write([]byte("Notification sent")) } func sendDiscordNotification(title, message string) error { - webhookURL := os.Getenv("WEBHOOK_URL") - if webhookURL == "" { - return fmt.Errorf("WEBHOOK_URL environment variable not set") - } + webhookURL := os.Getenv("WEBHOOK_URL") + if webhookURL == "" { + log.Printf("WEBHOOK_URL environment variable not set") + return fmt.Errorf("WEBHOOK_URL environment variable not set") + } // Discord webhook payload type discordPayload struct { - Content string `json:"content"` + Content string `json:"content"` } content := "**" + title + "**\n" + message @@ -63,18 +70,23 @@ func sendDiscordNotification(title, message string) error { jsonData, err := json.Marshal(payload) if err != nil { - return err + log.Printf("Failed to marshal Discord payload: %v", err) + return err } + log.Printf("Sending Discord notification: Title='%s', Message='%s'", title, message) resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(jsonData)) if err != nil { - return err + log.Printf("Error posting to Discord webhook: %v", err) + return err } defer resp.Body.Close() if resp.StatusCode < 200 || resp.StatusCode >= 300 { - return fmt.Errorf("Discord webhook returned status: %s", resp.Status) + log.Printf("Discord webhook returned status: %s", resp.Status) + return fmt.Errorf("Discord webhook returned status: %s", resp.Status) } + log.Printf("Discord notification sent successfully: Title='%s'", title) return nil }