From 9bbb9157fed7d6b2a05389630d301635f5ef9372 Mon Sep 17 00:00:00 2001 From: Simon Rieger Date: Tue, 1 Jul 2025 20:29:31 +0200 Subject: [PATCH] add logging --- main.go | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 219155e..16619ae 100644 --- a/main.go +++ b/main.go @@ -13,12 +13,12 @@ import ( // Konfiguration aus Umgebungsvariablen var ( - gotosocialURL = os.Getenv("GOTOSOCIAL_URL") - accessToken = os.Getenv("GOTOSOCIAL_TOKEN") - ntfyServer = os.Getenv("NTFY_SERVER") - ntfyToken = os.Getenv("NTFY_TOKEN") - ntfyTopic = os.Getenv("NTFY_TOPIC") - pollInterval, _ = time.ParseDuration(os.Getenv("POLL_INTERVAL")) + gotosocialURL = os.Getenv("GOTOSOCIAL_URL") + accessToken = os.Getenv("GOTOSOCIAL_TOKEN") + ntfyServer = os.Getenv("NTFY_SERVER") + ntfyToken = os.Getenv("NTFY_TOKEN") + ntfyTopic = os.Getenv("NTFY_TOPIC") + pollInterval, _ = time.ParseDuration(os.Getenv("POLL_INTERVAL")) ) type Notification struct { @@ -34,16 +34,25 @@ type Notification struct { } type NtfyMessage struct { - Topic string `json:"topic"` - Title string `json:"title"` - Message string `json:"message"` + Topic string `json:"topic"` + Title string `json:"title"` + Message string `json:"message"` Tags []string `json:"tags"` - Priority int `json:"priority"` + Priority int `json:"priority"` } func main() { + log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile) + + log.Println("Starte GoToSocial-Notifier...") + if pollInterval == 0 { pollInterval = 30 * time.Second + log.Printf("Kein POLL_INTERVAL gesetzt, verwende Default: %s", pollInterval) + } + + if gotosocialURL == "" || accessToken == "" || ntfyServer == "" || ntfyTopic == "" { + log.Fatal("Eine oder mehrere erforderliche Umgebungsvariablen fehlen (GOTOSOCIAL_URL, GOTOSOCIAL_TOKEN, NTFY_SERVER, NTFY_TOPIC)") } ticker := time.NewTicker(pollInterval) @@ -54,6 +63,7 @@ func main() { for { select { case <-ticker.C: + log.Println("Frage GoToSocial-Benachrichtigungen ab ...") notifications, err := fetchNotifications(lastID) if err != nil { log.Printf("Fehler beim Abrufen: %v", err) @@ -62,11 +72,16 @@ func main() { if len(notifications) > 0 { lastID = notifications[0].ID + log.Printf("%d neue Benachrichtigungen gefunden.", len(notifications)) + } else { + log.Println("Keine neuen Benachrichtigungen.") } for _, n := range notifications { if err := sendToNtfy(n); err != nil { log.Printf("Fehler beim Senden an ntfy: %v", err) + } else { + log.Printf("Benachrichtigung '%s' an ntfy gesendet.", n.ID) } } }