In handlers made use of a SongList.
This commit is contained in:
parent
8c1e46900a
commit
a4f7366213
@ -2,18 +2,24 @@ package http
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"dwelling-radio/internal/radio"
|
"dwelling-radio/internal/radio"
|
||||||
|
"dwelling-radio/pkg/oggtag"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var songList radio.SongList
|
||||||
|
|
||||||
type DJHandlers struct {
|
type DJHandlers struct {
|
||||||
listeners *radio.Listeners
|
listeners *radio.Listeners
|
||||||
playlist *radio.Playlist
|
playlist *radio.Playlist
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDJHandlers(l *radio.Listeners, p *radio.Playlist) *DJHandlers {
|
func NewDJHandlers(l *radio.Listeners, p *radio.Playlist, slLen int) *DJHandlers {
|
||||||
|
songList = *radio.NewSongList(slLen)
|
||||||
return &DJHandlers{listeners: l, playlist: p}
|
return &DJHandlers{listeners: l, playlist: p}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,6 +30,11 @@ func (dj *DJHandlers) ListenersGet(w http.ResponseWriter, _ *http.Request) {
|
|||||||
|
|
||||||
func (dj *DJHandlers) ListenersInc(w http.ResponseWriter, _ *http.Request) {
|
func (dj *DJHandlers) ListenersInc(w http.ResponseWriter, _ *http.Request) {
|
||||||
l := dj.listeners.Inc()
|
l := dj.listeners.Inc()
|
||||||
|
go func() {
|
||||||
|
if l > songList.Current().MaxListeners {
|
||||||
|
songList.Current().MaxListeners = l
|
||||||
|
}
|
||||||
|
}()
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
w.Header().Add("Content-Type", "text/plain")
|
w.Header().Add("Content-Type", "text/plain")
|
||||||
fmt.Fprint(w, l)
|
fmt.Fprint(w, l)
|
||||||
@ -45,6 +56,24 @@ func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) {
|
|||||||
nxt := dj.playlist.Next()
|
nxt := dj.playlist.Next()
|
||||||
if nxt == "" {
|
if nxt == "" {
|
||||||
log.Println("the end of a playlist has been reached")
|
log.Println("the end of a playlist has been reached")
|
||||||
|
} else {
|
||||||
|
go func() {
|
||||||
|
f, err := os.Open(nxt)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("cannot open file \"", nxt, "\" to read tags")
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := make([]byte, 4096)
|
||||||
|
f.Read(buf)
|
||||||
|
|
||||||
|
song := radio.Song_{
|
||||||
|
Artist: oggtag.OggGetTag(buf, "artist"),
|
||||||
|
Title: oggtag.OggGetTag(buf, "title"),
|
||||||
|
MaxListeners: dj.listeners.Current(),
|
||||||
|
StartAt: time.Now()}
|
||||||
|
// radio.CheckAndUpdateMostListenedSong(song, currentSong)
|
||||||
|
songList.Add(song)
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
fmt.Fprint(w, nxt)
|
fmt.Fprint(w, nxt)
|
||||||
}
|
}
|
||||||
@ -53,10 +82,14 @@ func (dj *DJHandlers) Song(w http.ResponseWriter, r *http.Request) {
|
|||||||
isList := r.URL.Query().Has("list")
|
isList := r.URL.Query().Has("list")
|
||||||
w.Header().Add("Content-Type", "application/json")
|
w.Header().Add("Content-Type", "application/json")
|
||||||
if isList {
|
if isList {
|
||||||
// TODO
|
err := json.NewEncoder(w).Encode(songList.List())
|
||||||
json.NewEncoder(w).Encode("")
|
if err != nil {
|
||||||
|
log.Println("DJHandlers.Song list:", err)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// TODO
|
err := json.NewEncoder(w).Encode(songList.Current())
|
||||||
json.NewEncoder(w).Encode("")
|
if err != nil {
|
||||||
|
log.Println("DJHandlers.Song current:", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user