notlistener/main.go
2024-03-06 13:59:27 +01:00

39 lines
838 B
Go

package main
import (
"encoding/json"
"log"
"net/http"
"github.com/codegoalie/golibnotify"
)
type Notify struct {
Summary string `json:"summary"`
Body string `json:"body"`
Icon string `json:"icon"`
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/api/notify", func(w http.ResponseWriter, r *http.Request) {
var n Notify
defer r.Body.Close()
if err := json.NewDecoder(r.Body).Decode(&n); err != nil {
w.WriteHeader(http.StatusBadRequest)
}
// Send the notification
sn := golibnotify.NewSimpleNotifier("notlistener")
if err := sn.Show(n.Summary, n.Body, n.Icon); err != nil {
log.Printf("Unable to send notification: %v", err)
}
})
log.Printf("Starting server on :8080")
if err := http.ListenAndServe(":8080", mux); err != nil {
log.Printf("Error starting listener: %v", err)
}
}