1
0

Added handlers used in a radiodj service. Currently just for listeners.

This commit is contained in:
Alexander Andreev 2023-09-30 23:59:48 +04:00
parent 61e2f6d8fd
commit 978a2602d8
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -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)
}