40 lines
929 B
Go
40 lines
929 B
Go
|
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)
|
||
|
}
|