1
0

Updated internal/http.

This commit is contained in:
Alexander Andreev 2024-05-10 00:07:07 +04:00
parent d936b41483
commit 71844a106d
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34
3 changed files with 36 additions and 67 deletions

View File

@ -2,6 +2,7 @@ package http
import (
"dwelling-radio/internal/radio"
"dwelling-radio/internal/statistics"
"dwelling-radio/pkg/oggtag"
"encoding/json"
"fmt"
@ -14,15 +15,16 @@ import (
type DJHandlers struct {
listeners *radio.ListenerCounter
playlist *radio.Playlist
songList *radio.SongList
mostLSong *radio.MostListenedSong
stats statistics.Statistics
curSong *radio.Song
listLen int64
fallbackSong string
}
func NewDJHandlers(l *radio.ListenerCounter, p *radio.Playlist,
sl *radio.SongList, mls *radio.MostListenedSong, fS string) *DJHandlers {
return &DJHandlers{listeners: l, playlist: p, songList: sl,
mostLSong: mls, fallbackSong: fS}
stats statistics.Statistics, cs *radio.Song, n int64, fS string) *DJHandlers {
return &DJHandlers{listeners: l, playlist: p,
stats: stats, curSong: cs, listLen: n, fallbackSong: fS}
}
func (dj *DJHandlers) ListenersUpdate(w http.ResponseWriter, r *http.Request) {
@ -41,7 +43,10 @@ func (dj *DJHandlers) ListenersUpdate(w http.ResponseWriter, r *http.Request) {
switch r.FormValue("action") {
case "listener_add":
l := dj.listeners.Inc()
go dj.songList.UpdateCurrentMaxListeners(l)
go func() {
dj.curSong.UpdateMaxListeners(l)
dj.curSong.IncListeners()
}()
case "listener_remove":
if _, err := dj.listeners.Dec(); err != nil {
log.Println("DJHandlers.ListenersUpdate:", err)
@ -78,41 +83,53 @@ func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) {
return
}
song := radio.Song{
newSong := radio.Song{
Artist: oggf.GetTag("artist"),
Title: oggf.GetTag("title"),
Duration: oggf.GetDuration(),
Listeners: dj.listeners.Current(),
MaxListeners: dj.listeners.Current(),
// Here 5 seconds are being added because it is approximately the
// time between the creation of this Song object and when ezstream
// actually starts to play it.
StartAt: time.Now().Add(5 * time.Second)}
if newSong.Artist == "" || newSong.Title == "" {
log.Println("Playlist:", nxt, "has no artist or title tags.")
}
if strings.HasSuffix(nxt, "/fallback.ogg") {
song.Artist = "Nothing to play. Playing a fallback: " + song.Artist
newSong.Artist = "Nothing to play. Playing a fallback: " + newSong.Artist
}
if dj.songList.Current() != nil {
dj.mostLSong.Update(*dj.songList.Current())
if dj.curSong.Artist != "" {
if err := dj.stats.Add(dj.curSong); err != nil {
log.Println("cannot add a song to a stats DB:", err)
}
}
dj.songList.Add(song)
dj.curSong.SetFrom(&newSong)
}()
fmt.Fprintln(w, nxt)
}
func (dj *DJHandlers) Status(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(&struct {
Current *radio.Song `json:"current_song,omitempty"`
Listeners *radio.ListenerCounter `json:"listeners"`
List []radio.Song `json:"last_songs,omitempty"`
Mls *radio.MostListenedSong `json:"most_listened_song,omitempty"`
lst, err := dj.stats.LastNSongs(dj.listLen)
if err != nil {
log.Println("failed to fetch last n songs:", err)
}
err = json.NewEncoder(w).Encode(&struct {
Current *radio.Song `json:"current_song,omitempty"`
Listeners *radio.ListenerCounter `json:"listeners"`
List []radio.Song `json:"last_songs,omitempty"`
}{
Current: dj.songList.Current(),
Current: dj.curSong,
Listeners: dj.listeners,
List: dj.songList.List(),
Mls: dj.mostLSong.Get()})
List: lst})
if err != nil {
log.Println("DJHandlers.Status:", err)
http.Error(w, "status parsing failed", http.StatusInternalServerError)

View File

@ -1,48 +0,0 @@
package http
import (
"dwelling-radio/internal/radio"
"dwelling-radio/pkg/utils"
"dwelling-radio/web"
"net/http"
"os"
)
type Handlers struct {
songList *radio.SongList
listeners *radio.ListenerCounter
mostLSong *radio.MostListenedSong
filelistPath string
}
func NewHandlers(filelistPath string, songList *radio.SongList, listeners *radio.ListenerCounter, mls *radio.MostListenedSong) *Handlers {
return &Handlers{
songList: songList,
filelistPath: filelistPath,
listeners: listeners,
mostLSong: mls}
}
func (h *Handlers) Index(w http.ResponseWriter, r *http.Request) {
web.Index(utils.MainSite(r.Host), h.songList, h.listeners, h.mostLSong.Get(), r, w)
}
func (h *Handlers) Filelist(w http.ResponseWriter, _ *http.Request) {
w.Header().Add("Content-Type", "text/html")
data, _ := os.ReadFile(h.filelistPath)
w.Write(data)
}
func ServeAsset(path, mime, attachement string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if mime != "" {
w.Header().Add("Content-Type", mime)
}
if attachement != "" {
w.Header().Add("Content-Disposition", "attachment; filename=\""+path+"\"")
}
w.Write(web.AssetsGetFile(path))
}
}