9b0b6b1ecf
- ntfy - keyserver - webhook - webserver functional modules.
46 lines
993 B
Go
46 lines
993 B
Go
package ntfy
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
URL string
|
|
Topic string
|
|
}
|
|
|
|
// Notify sends a failure notification. Silently does nothing if
|
|
// URL or Topic are unset, so callers don't need to check first.
|
|
func Notify(cfg Config, context string, err error) {
|
|
if cfg.URL == "" || cfg.Topic == "" {
|
|
return
|
|
}
|
|
|
|
msg := fmt.Sprintf("%s: %v", context, err)
|
|
url := fmt.Sprintf("%s/%s", cfg.URL, cfg.Topic)
|
|
|
|
req, reqErr := http.NewRequest("POST", url, bytes.NewBufferString(msg))
|
|
if reqErr != nil {
|
|
log.Printf("ntfy request build failed: %v", reqErr)
|
|
return
|
|
}
|
|
req.Header.Set("Title", "Varde Notification")
|
|
req.Header.Set("Priority", "high")
|
|
|
|
client := &http.Client{Timeout: 5 * time.Second}
|
|
resp, sendErr := client.Do(req)
|
|
if sendErr != nil {
|
|
log.Printf("ntfy notification failed: %v", sendErr)
|
|
return
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode >= 300 {
|
|
log.Printf("ntfy notification returned status %d", resp.StatusCode)
|
|
}
|
|
}
|