handlers moved to internal/http. Logging changed to std log. Type RadioHandlers renamed to Handlers.
This commit is contained in:
parent
91329ceaec
commit
7c5e1465af
@ -1,37 +1,34 @@
|
|||||||
package handlers
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"dwelling-radio/internal/configuration"
|
"dwelling-radio/internal/configuration"
|
||||||
"dwelling-radio/internal/radio"
|
"dwelling-radio/internal/radio"
|
||||||
"dwelling-radio/pkg/logging"
|
|
||||||
"dwelling-radio/pkg/utils"
|
"dwelling-radio/pkg/utils"
|
||||||
"dwelling-radio/web"
|
"dwelling-radio/web"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const FormatISO8601 = "2006-01-02T15:04:05-0700"
|
const FormatISO8601 = "2006-01-02T15:04:05-0700"
|
||||||
|
|
||||||
type RadioHandlers struct {
|
type Handlers struct {
|
||||||
conf *configuration.Configuration
|
conf *configuration.Configuration
|
||||||
logErr *logging.Logger
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRadioHandlers(conf *configuration.Configuration, lErr *logging.Logger) *RadioHandlers {
|
func NewHandlers(conf *configuration.Configuration) *Handlers {
|
||||||
return &RadioHandlers{
|
return &Handlers{conf: conf}
|
||||||
conf: conf,
|
|
||||||
logErr: lErr}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *RadioHandlers) AssetsFS() http.FileSystem {
|
func (h *Handlers) AssetsFS() http.FileSystem {
|
||||||
return web.Assets()
|
return web.Assets()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *RadioHandlers) Index(w http.ResponseWriter, r *http.Request) {
|
func (h *Handlers) Index(w http.ResponseWriter, r *http.Request) {
|
||||||
status, err := radio.IcecastGetStatus(h.conf.Icecast.URL)
|
status, err := radio.IcecastGetStatus(h.conf.Icecast.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.logErr.Println("failed to get Icecast status:", err)
|
log.Println("failed to get Icecast status:", err)
|
||||||
} else {
|
} else {
|
||||||
if tim, err := time.Parse(time.RFC1123Z, status.ServerStartDate); err == nil {
|
if tim, err := time.Parse(time.RFC1123Z, status.ServerStartDate); err == nil {
|
||||||
status.ServerStartDate = utils.ToClientTimezone(tim, r).Format(time.RFC1123)
|
status.ServerStartDate = utils.ToClientTimezone(tim, r).Format(time.RFC1123)
|
||||||
@ -45,7 +42,7 @@ func (h *RadioHandlers) Index(w http.ResponseWriter, r *http.Request) {
|
|||||||
songs, err := radio.IcecastLastPlayedSongs(h.conf.ListLastNSongs,
|
songs, err := radio.IcecastLastPlayedSongs(h.conf.ListLastNSongs,
|
||||||
h.conf.Icecast.Playlist)
|
h.conf.Icecast.Playlist)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.logErr.Println("cannot retrieve last songs:", err)
|
log.Println("cannot retrieve last songs:", err)
|
||||||
} else {
|
} else {
|
||||||
for i := 0; i < len(songs); i++ {
|
for i := 0; i < len(songs); i++ {
|
||||||
if tim, err := time.Parse(radio.SongTimeFormat, songs[i].Time); err == nil {
|
if tim, err := time.Parse(radio.SongTimeFormat, songs[i].Time); err == nil {
|
||||||
@ -57,10 +54,10 @@ func (h *RadioHandlers) Index(w http.ResponseWriter, r *http.Request) {
|
|||||||
web.Index(utils.MainSite(r.Host), status, &songs, w)
|
web.Index(utils.MainSite(r.Host), status, &songs, w)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *RadioHandlers) Status(w http.ResponseWriter, r *http.Request) {
|
func (h *Handlers) Status(w http.ResponseWriter, r *http.Request) {
|
||||||
status, err := radio.IcecastGetStatus(h.conf.Icecast.URL)
|
status, err := radio.IcecastGetStatus(h.conf.Icecast.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.logErr.Println("cannot retrieve Icecast status:", err)
|
log.Println("cannot retrieve Icecast status:", err)
|
||||||
http.Error(w, "cannot retrieve Icecast status", http.StatusInternalServerError)
|
http.Error(w, "cannot retrieve Icecast status", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -77,10 +74,10 @@ func (h *RadioHandlers) Status(w http.ResponseWriter, r *http.Request) {
|
|||||||
json.NewEncoder(w).Encode(status)
|
json.NewEncoder(w).Encode(status)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *RadioHandlers) LastSong(w http.ResponseWriter, r *http.Request) {
|
func (h *Handlers) LastSong(w http.ResponseWriter, r *http.Request) {
|
||||||
song, err := radio.IcecastLastSong(h.conf.Icecast.Playlist)
|
song, err := radio.IcecastLastSong(h.conf.Icecast.Playlist)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.logErr.Println("cannot retrieve last songs:", err)
|
log.Println("cannot retrieve last songs:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if song.Time == "" {
|
if song.Time == "" {
|
||||||
@ -96,7 +93,7 @@ func (h *RadioHandlers) LastSong(w http.ResponseWriter, r *http.Request) {
|
|||||||
json.NewEncoder(w).Encode(song)
|
json.NewEncoder(w).Encode(song)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *RadioHandlers) Playlist(w http.ResponseWriter, _ *http.Request) {
|
func (h *Handlers) Playlist(w http.ResponseWriter, _ *http.Request) {
|
||||||
w.Header().Add("Content-Disposition", "attachment; filename=\"radio.arav.top.m3u\"")
|
w.Header().Add("Content-Disposition", "attachment; filename=\"radio.arav.top.m3u\"")
|
||||||
fc, _ := web.AssetsGetFile("radio.arav.top.m3u")
|
fc, _ := web.AssetsGetFile("radio.arav.top.m3u")
|
||||||
w.Write(fc)
|
w.Write(fc)
|
Loading…
Reference in New Issue
Block a user