add logging
This commit is contained in:
parent
ca377f660b
commit
9bbb9157fe
1 changed files with 25 additions and 10 deletions
35
main.go
35
main.go
|
@ -13,12 +13,12 @@ import (
|
||||||
|
|
||||||
// Konfiguration aus Umgebungsvariablen
|
// Konfiguration aus Umgebungsvariablen
|
||||||
var (
|
var (
|
||||||
gotosocialURL = os.Getenv("GOTOSOCIAL_URL")
|
gotosocialURL = os.Getenv("GOTOSOCIAL_URL")
|
||||||
accessToken = os.Getenv("GOTOSOCIAL_TOKEN")
|
accessToken = os.Getenv("GOTOSOCIAL_TOKEN")
|
||||||
ntfyServer = os.Getenv("NTFY_SERVER")
|
ntfyServer = os.Getenv("NTFY_SERVER")
|
||||||
ntfyToken = os.Getenv("NTFY_TOKEN")
|
ntfyToken = os.Getenv("NTFY_TOKEN")
|
||||||
ntfyTopic = os.Getenv("NTFY_TOPIC")
|
ntfyTopic = os.Getenv("NTFY_TOPIC")
|
||||||
pollInterval, _ = time.ParseDuration(os.Getenv("POLL_INTERVAL"))
|
pollInterval, _ = time.ParseDuration(os.Getenv("POLL_INTERVAL"))
|
||||||
)
|
)
|
||||||
|
|
||||||
type Notification struct {
|
type Notification struct {
|
||||||
|
@ -34,16 +34,25 @@ type Notification struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type NtfyMessage struct {
|
type NtfyMessage struct {
|
||||||
Topic string `json:"topic"`
|
Topic string `json:"topic"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Tags []string `json:"tags"`
|
Tags []string `json:"tags"`
|
||||||
Priority int `json:"priority"`
|
Priority int `json:"priority"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile)
|
||||||
|
|
||||||
|
log.Println("Starte GoToSocial-Notifier...")
|
||||||
|
|
||||||
if pollInterval == 0 {
|
if pollInterval == 0 {
|
||||||
pollInterval = 30 * time.Second
|
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)
|
ticker := time.NewTicker(pollInterval)
|
||||||
|
@ -54,6 +63,7 @@ func main() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
|
log.Println("Frage GoToSocial-Benachrichtigungen ab ...")
|
||||||
notifications, err := fetchNotifications(lastID)
|
notifications, err := fetchNotifications(lastID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Fehler beim Abrufen: %v", err)
|
log.Printf("Fehler beim Abrufen: %v", err)
|
||||||
|
@ -62,11 +72,16 @@ func main() {
|
||||||
|
|
||||||
if len(notifications) > 0 {
|
if len(notifications) > 0 {
|
||||||
lastID = notifications[0].ID
|
lastID = notifications[0].ID
|
||||||
|
log.Printf("%d neue Benachrichtigungen gefunden.", len(notifications))
|
||||||
|
} else {
|
||||||
|
log.Println("Keine neuen Benachrichtigungen.")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, n := range notifications {
|
for _, n := range notifications {
|
||||||
if err := sendToNtfy(n); err != nil {
|
if err := sendToNtfy(n); err != nil {
|
||||||
log.Printf("Fehler beim Senden an ntfy: %v", err)
|
log.Printf("Fehler beim Senden an ntfy: %v", err)
|
||||||
|
} else {
|
||||||
|
log.Printf("Benachrichtigung '%s' an ntfy gesendet.", n.ID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue