1
0

Fixed listeners number update, well, on-dis/connect in mount is for a SOURCE. To get listeners you need to use <authentication type="url">.

Also, disabled all listener handler, added POST /api/listener/icecast handler.
This commit is contained in:
Alexander Andreev 2023-10-08 22:17:56 +04:00
parent 635fc4ec1d
commit dbf71f3f64
Signed by: Arav
GPG Key ID: D22A817D95815393
4 changed files with 48 additions and 7 deletions

View File

@ -68,13 +68,14 @@ func main() {
djh := ihttp.NewDJHandlers(lstnrs, plylst, songList, &mostListenedSong, *fallbackSong)
s := r.Sub("/api/listener")
s.Handler(http.MethodGet, "/", djh.ListenersGet)
s.Handler(http.MethodPost, "/", djh.ListenersInc)
s.Handler(http.MethodDelete, "/", djh.ListenersDec)
// s := r.Sub("/api/listener")
// s.Handler(http.MethodGet, "/", djh.ListenersGet)
// s.Handler(http.MethodPost, "/", djh.ListenersInc)
// s.Handler(http.MethodDelete, "/", djh.ListenersDec)
s = r.Sub("/api")
s := r.Sub("/api")
s.Handler(http.MethodPost, "/listener/icecast", djh.ListenersUpdate)
s.Handler(http.MethodGet, "/playlist", djh.PlaylistNext)
s.Handler(http.MethodGet, "/status", djh.Status)
// s.Handler(http.MethodGet, "/song", djh.Song)

View File

@ -40,8 +40,11 @@
<mount-name>/stream.ogg</mount-name>
<charset>UTF8</charset>
<public>1</public>
<on-connect>/usr/bin/dwelling-radio-listener-connect</on-connect>
<on-disconnect>/usr/bin/dwelling-radio-listener-disconnect</on-disconnect>
<authentication type="url">
<option name="listener_add" value="http://radio.arav.su/api/listener/icecast"/>
<option name="listener_remove" value="http://radio.arav.su/api/listener/icecast"/>
<option name="auth_header" value="Icecast-Auth-User: 1"/>
</authentication>
</mount>
<mount type="normal">

View File

@ -36,10 +36,12 @@ server {
}
location /api/listener {
allow 192.168.144.2;
deny all;
}
location /api/playlist {
allow 192.168.144.2;
deny all;
}
}

View File

@ -25,6 +25,41 @@ func NewDJHandlers(l *radio.ListenerCounter, p *radio.Playlist,
mostLSong: mls, fallbackSong: fS}
}
func (dj *DJHandlers) ListenersUpdate(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
log.Println("DJHandlers.ListenersUpdate panic:", err)
}
}()
if err := r.ParseForm(); err != nil {
log.Println("DJHandlers.ListenersUpdate:", err)
http.Error(w, "cannot parse form", http.StatusBadRequest)
return
}
fmt.Println(r.Method, r.Form)
switch r.FormValue("action") {
case "listener_add":
l := dj.listeners.Inc()
go dj.songList.UpdateCurrentMaxListeners(l)
case "listener_remove":
if _, err := dj.listeners.Dec(); err != nil {
log.Println("DJHandlers.ListenersUpdate:", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
default:
w.WriteHeader(http.StatusNotAcceptable)
return
}
w.Header().Add("Content-Type", "text/plain")
w.Header().Add("Icecast-Auth-User", "1")
w.WriteHeader(http.StatusOK)
}
func (dj *DJHandlers) ListenersGet(w http.ResponseWriter, _ *http.Request) {
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(dj.listeners)