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

49 lines
1.2 KiB
Go
Raw Normal View History

package http
2022-03-08 01:17:24 +04:00
import (
"dwelling-radio/internal/radio"
"dwelling-radio/pkg/utils"
"dwelling-radio/web"
2022-03-08 01:17:24 +04:00
"net/http"
"os"
2022-03-08 01:17:24 +04:00
)
type Handlers struct {
songList *radio.SongList
listeners *radio.ListenerCounter
2023-10-08 00:52:40 +04:00
mostLSong *radio.MostListenedSong
filelistPath string
2022-03-08 01:17:24 +04:00
}
2023-10-08 00:52:40 +04:00
func NewHandlers(filelistPath string, songList *radio.SongList, listeners *radio.ListenerCounter, mls *radio.MostListenedSong) *Handlers {
return &Handlers{
songList: songList,
filelistPath: filelistPath,
2023-10-08 00:52:40 +04:00
listeners: listeners,
mostLSong: mls}
2022-03-08 01:17:24 +04:00
}
func (h *Handlers) Index(w http.ResponseWriter, r *http.Request) {
2023-10-08 00:52:40 +04:00
web.Index(utils.MainSite(r.Host), h.songList, h.listeners, h.mostLSong.Get(), r, w)
2022-03-08 01:17:24 +04:00
}
func (h *Handlers) Filelist(w http.ResponseWriter, _ *http.Request) {
2023-02-19 22:26:25 +04:00
w.Header().Add("Content-Type", "text/html")
data, _ := os.ReadFile(h.filelistPath)
w.Write(data)
}
2023-02-19 21:22:56 +04:00
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)
}
2023-05-21 22:35:37 +04:00
if attachement != "" {
w.Header().Add("Content-Disposition", "attachment; filename=\""+path+"\"")
}
2023-05-21 22:44:09 +04:00
w.Write(web.AssetsGetFile(path))
}
2023-05-21 22:44:09 +04:00
}