From 978a2602d8abc8bb557e2add91843700cacf877b Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sat, 30 Sep 2023 23:59:48 +0400 Subject: [PATCH] Added handlers used in a radiodj service. Currently just for listeners. --- internal/http/dj_handlers.go | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 internal/http/dj_handlers.go diff --git a/internal/http/dj_handlers.go b/internal/http/dj_handlers.go new file mode 100644 index 0000000..adc26cb --- /dev/null +++ b/internal/http/dj_handlers.go @@ -0,0 +1,39 @@ +package http + +import ( + "dwelling-radio/internal/radio" + "fmt" + "log" + "net/http" +) + +type DJHandlers struct { + listeners *radio.Listeners +} + +func NewDJHandlers(listeners *radio.Listeners) *DJHandlers { + return &DJHandlers{listeners: listeners} +} + +func (dj *DJHandlers) ListenersGet(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "text/plain") + fmt.Fprint(w, dj.listeners.Current(), dj.listeners.Peak()) +} + +func (dj *DJHandlers) ListenersInc(w http.ResponseWriter, r *http.Request) { + l := dj.listeners.Inc() + w.WriteHeader(http.StatusCreated) + w.Header().Add("Content-Type", "text/plain") + fmt.Fprint(w, l) +} + +func (dj *DJHandlers) ListenersDec(w http.ResponseWriter, r *http.Request) { + l, err := dj.listeners.Dec() + if err != nil { + log.Print(err) + http.Error(w, err.Error(), http.StatusBadRequest) + } + w.WriteHeader(http.StatusOK) + w.Header().Add("Content-Type", "text/plain") + fmt.Fprint(w, l) +}