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