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

157 lines
3.9 KiB
Go
Raw Normal View History

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"
"embed"
"encoding/json"
"fmt"
"html/template"
"io/fs"
"net/http"
"time"
2022-03-08 01:17:24 +04:00
"github.com/Joker/jade"
)
const FormatISO8601 = "2006-01-02T15:04:05-0700"
2022-03-08 01:17:24 +04:00
var compiledTemplates map[string]*template.Template
//go:embed web/assets
var assetsDir embed.FS
//go:embed web/templates
var templatesDir embed.FS
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 {
compileTemplates(lErr)
return &RadioHandlers{
conf: conf,
logErr: lErr}
}
func (h *RadioHandlers) AssetsFS() http.FileSystem {
f, _ := fs.Sub(assetsDir, "web/assets")
return http.FS(f)
}
func (h *RadioHandlers) Index(w http.ResponseWriter, r *http.Request) {
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)
status = &radio.IcecastStatus{}
return
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)
}
if tim, err := time.Parse(time.RFC1123Z, status.ServerStartDate); err == nil {
status.ServerStartDate = utils.ToClientTimezone(tim, r).Format(time.RFC1123Z)
}
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
if err := compiledTemplates["index"].Execute(w, &IndexData{
MainSite: utils.MainSite(r.Host),
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)
http.Error(w, "cannot execute Index template", http.StatusInternalServerError)
2022-03-08 01:17:24 +04:00
}
}
func (h *RadioHandlers) Stats(w http.ResponseWriter, r *http.Request) {
status, err := radio.IcecastGetStatus(h.conf.Icecast.URL)
2022-03-08 01:17:24 +04:00
if err != nil {
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
}
if tim, err := time.Parse(time.RFC1123Z, status.ServerStartDate); err == nil {
status.ServerStartDate = utils.ToClientTimezone(tim, r).Format(time.RFC1123Z)
}
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")
json.NewEncoder(w).Encode(status)
2022-03-08 01:17:24 +04:00
}
func (h *RadioHandlers) LastSong(w http.ResponseWriter, r *http.Request) {
2022-03-30 20:20:30 +04:00
songs, err := radio.IcecastLastPlayedSongs(1, h.conf.Icecast.Playlist)
if err != nil {
h.logErr.Println("cannot retrieve last songs:", err)
}
if len(songs) == 0 {
w.WriteHeader(http.StatusNotFound)
return
}
2022-03-31 02:15:16 +04:00
for _, song := range songs {
if tim, err := time.Parse("15:04-0700", song.Time); err == nil {
song.Time = utils.ToClientTimezone(tim, r).Format("15:04")
}
}
2022-03-31 02:15:16 +04:00
w.Header().Add("Content-Type", "application/json")
2022-03-08 01:17:24 +04:00
json.NewEncoder(w).Encode(songs[0])
}
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\"")
pf, _ := assetsDir.Open("radio.arav.top.m3u")
defer pf.Close()
fmt.Fprint(w, pf)
}
func compileTemplates(lErr *logging.Logger) {
compiledTemplates = make(map[string]*template.Template)
t, _ := fs.Sub(templatesDir, "web/templates")
templatesFS := http.FS(t)
indexStr, err := jade.ParseFileFromFileSystem("index.jade", templatesFS)
if err != nil {
lErr.Fatalln(err)
}
indexTpl, err := template.New("index").Parse(indexStr)
if err != nil {
lErr.Fatalln(err)
}
compiledTemplates["index"] = indexTpl
}