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

113 lines
3.2 KiB
Go
Raw Normal View History

package http
2022-03-08 01:17:24 +04:00
import (
"dwelling-radio/internal/configuration"
"dwelling-radio/internal/radio"
"dwelling-radio/pkg/utils"
"dwelling-radio/web"
2022-03-08 01:17:24 +04:00
"encoding/json"
"log"
2022-03-08 01:17:24 +04:00
"net/http"
"os"
"time"
2022-03-08 01:17:24 +04:00
)
const FormatISO8601 = "2006-01-02T15:04:05-0700"
type Handlers struct {
conf *configuration.Configuration
2022-03-08 01:17:24 +04:00
}
func NewHandlers(conf *configuration.Configuration) *Handlers {
return &Handlers{conf: conf}
2022-03-08 01:17:24 +04:00
}
func (h *Handlers) AssetsFS() http.FileSystem {
return web.Assets()
2022-03-08 01:17:24 +04:00
}
func (h *Handlers) 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 {
log.Println("failed to get Icecast status:", err)
} 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 {
log.Println("cannot retrieve last songs:", err)
} 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")
}
}
}
web.Index(utils.MainSite(r.Host), h.conf.ListLastNSongs, status, &songs, w)
2022-03-08 01:17:24 +04:00
}
func (h *Handlers) Status(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 {
log.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.RFC1123)
}
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 *Handlers) LastSong(w http.ResponseWriter, r *http.Request) {
song, err := radio.IcecastLastSong(h.conf.Icecast.Playlist)
2022-03-30 20:20:30 +04:00
if err != nil {
log.Println("cannot retrieve last songs:", err)
2022-03-30 20:20:30 +04:00
}
if song.Time == "" {
w.WriteHeader(http.StatusNotFound)
return
}
2022-03-31 02:15:16 +04:00
if tim, err := time.Parse(radio.SongTimeFormat, 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")
json.NewEncoder(w).Encode(song)
2022-03-08 01:17:24 +04:00
}
func (h *Handlers) 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")
w.Write(fc)
2022-03-08 01:17:24 +04:00
}
func (h *Handlers) Filelist(w http.ResponseWriter, _ *http.Request) {
w.Header().Add("Content-Type", "plain/html")
data, _ := os.ReadFile(h.conf.FilelistPath)
w.Write(data)
}
2023-02-19 21:22:56 +04:00
func (h *Handlers) RobotsTxt(w http.ResponseWriter, _ *http.Request) {
w.Header().Add("Content-Disposition", "attachment; filename=\"robots.txt\"")
w.Write([]byte("User-agent: *\nDisallow: /assets/\nDisallow: /live/"))
}