package api import ( "net/http" "github.com/go-chi/chi/v5" "github.com/cdnutter/playlink/internal/notify" ) func (s *Server) notifyRoutes(r chi.Router) { r.Get("/notifications", s.handleGetNotify) r.Put("/notifications", s.handlePutNotify) r.Post("/notifications/test", s.handleTestNotify) } func (s *Server) handleGetNotify(w http.ResponseWriter, r *http.Request) { writeJSON(w, 200, s.cfg.Notifier.Config(r.Context())) } func (s *Server) handlePutNotify(w http.ResponseWriter, r *http.Request) { var c notify.Config if !readJSON(w, r, &c) { return } if err := s.cfg.Notifier.SetConfig(r.Context(), c); err != nil { writeErr(w, 400, err) return } writeJSON(w, 200, s.cfg.Notifier.Config(r.Context())) } // handleTestNotify sends a test message to a target as entered, saved or not. func (s *Server) handleTestNotify(w http.ResponseWriter, r *http.Request) { var body struct { Target notify.Target `json:"target"` BaseURL string `json:"base_url"` } if !readJSON(w, r, &body) { return } if err := s.cfg.Notifier.Test(r.Context(), body.Target, body.BaseURL); err != nil { writeErr(w, 400, err) return } writeJSON(w, 200, map[string]bool{"ok": true}) }