add logging

This commit is contained in:
Simon Rieger 2025-07-01 20:29:31 +02:00
parent ca377f660b
commit 9bbb9157fe

35
main.go
View file

@ -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)
}
}
}