2022-03-08 01:17:24 +04:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"dwelling-radio/internal/configuration"
|
|
|
|
"dwelling-radio/internal/radio"
|
|
|
|
"dwelling-radio/pkg/logging"
|
|
|
|
"dwelling-radio/pkg/utils"
|
2022-05-24 18:29:15 +04:00
|
|
|
"dwelling-radio/web"
|
2022-03-08 01:17:24 +04:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2022-03-31 02:55:34 +04:00
|
|
|
"time"
|
2022-03-08 01:17:24 +04:00
|
|
|
)
|
|
|
|
|
2022-03-31 02:55:34 +04:00
|
|
|
const FormatISO8601 = "2006-01-02T15:04:05-0700"
|
|
|
|
|
2022-03-08 01:17:24 +04:00
|
|
|
type NotFoundData struct {
|
|
|
|
MainSite string
|
|
|
|
}
|
|
|
|
|
|
|
|
type IndexData struct {
|
|
|
|
MainSite string
|
|
|
|
Status *radio.IcecastStatus
|
|
|
|
Songs []radio.Song
|
|
|
|
}
|
|
|
|
|
|
|
|
type RadioHandlers struct {
|
|
|
|
conf *configuration.Configuration
|
|
|
|
logErr *logging.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRadioHandlers(conf *configuration.Configuration, lErr *logging.Logger) *RadioHandlers {
|
2022-05-24 18:29:15 +04:00
|
|
|
web.CompileTemplates(lErr)
|
2022-03-08 01:17:24 +04:00
|
|
|
|
|
|
|
return &RadioHandlers{
|
|
|
|
conf: conf,
|
|
|
|
logErr: lErr}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RadioHandlers) AssetsFS() http.FileSystem {
|
2022-05-24 18:29:15 +04:00
|
|
|
return web.Assets()
|
2022-03-08 01:17:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RadioHandlers) Index(w http.ResponseWriter, r *http.Request) {
|
2022-03-30 19:13:55 +04:00
|
|
|
status, err := radio.IcecastGetStatus(h.conf.Icecast.URL)
|
2022-03-08 01:17:24 +04:00
|
|
|
if err != nil {
|
|
|
|
h.logErr.Println("failed to get Icecast status:", err)
|
2022-05-24 18:26:59 +04:00
|
|
|
} else {
|
|
|
|
if tim, err := time.Parse(time.RFC1123Z, status.ServerStartDate); err == nil {
|
|
|
|
status.ServerStartDate = utils.ToClientTimezone(tim, r).Format(time.RFC1123)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tim, err := time.Parse(FormatISO8601, status.ServerStartISO8601); err == nil {
|
|
|
|
status.ServerStartISO8601 = utils.ToClientTimezone(tim, r).Format(FormatISO8601)
|
|
|
|
}
|
2022-03-08 01:17:24 +04:00
|
|
|
}
|
|
|
|
|
2022-03-30 20:20:30 +04:00
|
|
|
songs, err := radio.IcecastLastPlayedSongs(h.conf.ListLastNSongs,
|
|
|
|
h.conf.Icecast.Playlist)
|
|
|
|
if err != nil {
|
|
|
|
h.logErr.Println("cannot retrieve last songs:", err)
|
2022-05-24 18:26:59 +04:00
|
|
|
} else {
|
|
|
|
for i := 0; i < len(songs); i++ {
|
|
|
|
if tim, err := time.Parse(radio.SongTimeFormat, songs[i].Time); err == nil {
|
|
|
|
songs[i].Time = utils.ToClientTimezone(tim, r).Format("15:04")
|
|
|
|
}
|
2022-03-31 04:02:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 19:41:04 +04:00
|
|
|
if err := web.IndexTemplate.Execute(w, &IndexData{
|
2022-03-08 01:17:24 +04:00
|
|
|
MainSite: utils.MainSite(r.Host),
|
2022-03-30 19:13:55 +04:00
|
|
|
Status: status,
|
2022-03-30 20:20:30 +04:00
|
|
|
Songs: songs,
|
2022-03-08 01:17:24 +04:00
|
|
|
}); err != nil {
|
|
|
|
h.logErr.Fatalln("failed to execute Index template:", err)
|
2022-03-30 19:13:55 +04:00
|
|
|
http.Error(w, "cannot execute Index template", http.StatusInternalServerError)
|
2022-03-08 01:17:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 18:41:50 +04:00
|
|
|
func (h *RadioHandlers) Status(w http.ResponseWriter, r *http.Request) {
|
2022-03-30 19:13:55 +04:00
|
|
|
status, err := radio.IcecastGetStatus(h.conf.Icecast.URL)
|
2022-03-08 01:17:24 +04:00
|
|
|
if err != nil {
|
2022-03-30 19:13:55 +04:00
|
|
|
h.logErr.Println("cannot retrieve Icecast status:", err)
|
|
|
|
http.Error(w, "cannot retrieve Icecast status", http.StatusInternalServerError)
|
|
|
|
return
|
2022-03-08 01:17:24 +04:00
|
|
|
}
|
|
|
|
|
2022-03-31 02:55:34 +04:00
|
|
|
if tim, err := time.Parse(time.RFC1123Z, status.ServerStartDate); err == nil {
|
2022-04-01 20:43:10 +04:00
|
|
|
status.ServerStartDate = utils.ToClientTimezone(tim, r).Format(time.RFC1123)
|
2022-03-31 02:55:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if tim, err := time.Parse(FormatISO8601, status.ServerStartISO8601); err == nil {
|
|
|
|
status.ServerStartISO8601 = utils.ToClientTimezone(tim, r).Format(FormatISO8601)
|
|
|
|
}
|
|
|
|
|
2022-03-31 02:15:16 +04:00
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2022-03-30 19:13:55 +04:00
|
|
|
json.NewEncoder(w).Encode(status)
|
2022-03-08 01:17:24 +04:00
|
|
|
}
|
|
|
|
|
2022-03-31 02:55:34 +04:00
|
|
|
func (h *RadioHandlers) LastSong(w http.ResponseWriter, r *http.Request) {
|
2022-03-31 15:39:51 +04:00
|
|
|
song, err := radio.IcecastLastSong(h.conf.Icecast.Playlist)
|
2022-03-30 20:20:30 +04:00
|
|
|
if err != nil {
|
|
|
|
h.logErr.Println("cannot retrieve last songs:", err)
|
|
|
|
}
|
|
|
|
|
2022-03-31 18:24:41 +04:00
|
|
|
if song.Time == "" {
|
2022-03-30 19:13:55 +04:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2022-03-31 02:15:16 +04:00
|
|
|
|
2022-04-02 04:29:58 +04:00
|
|
|
if tim, err := time.Parse(radio.SongTimeFormat, song.Time); err == nil {
|
2022-03-31 15:39:51 +04:00
|
|
|
song.Time = utils.ToClientTimezone(tim, r).Format("15:04")
|
2022-03-31 02:55:34 +04:00
|
|
|
}
|
|
|
|
|
2022-03-31 02:15:16 +04:00
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2022-03-31 15:39:51 +04:00
|
|
|
json.NewEncoder(w).Encode(song)
|
2022-03-08 01:17:24 +04:00
|
|
|
}
|
|
|
|
|
2022-03-30 19:13:55 +04:00
|
|
|
func (h *RadioHandlers) Playlist(w http.ResponseWriter, _ *http.Request) {
|
2022-03-08 01:17:24 +04:00
|
|
|
w.Header().Add("Content-Disposition", "attachment; filename=\"radio.arav.top.m3u\"")
|
2022-05-24 19:41:04 +04:00
|
|
|
fc, _ := web.AssetsGetFile("radio.arav.top.m3u")
|
2022-03-31 18:34:33 +04:00
|
|
|
w.Write(fc)
|
2022-03-08 01:17:24 +04:00
|
|
|
}
|