1
0
dwelling-radio/internal/http/handlers.go

49 lines
1.2 KiB
Go

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))
}
}