2023-02-07 02:27:30 +04:00
|
|
|
package http
|
2022-03-08 01:17:24 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"dwelling-radio/internal/radio"
|
|
|
|
"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"
|
2023-02-07 02:27:30 +04:00
|
|
|
"log"
|
2022-03-08 01:17:24 +04:00
|
|
|
"net/http"
|
2023-02-19 21:18:36 +04:00
|
|
|
"os"
|
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"
|
|
|
|
|
2023-02-07 02:27:30 +04:00
|
|
|
type Handlers struct {
|
2023-08-06 03:17:18 +04:00
|
|
|
icecastUrl string
|
|
|
|
icecastPlaylistPath string
|
|
|
|
songListLen int
|
|
|
|
filelistPath string
|
2022-03-08 01:17:24 +04:00
|
|
|
}
|
|
|
|
|
2023-08-06 03:17:18 +04:00
|
|
|
func NewHandlers(icecastUrl, icecastPlaylistPath, filelistPath string, songListLen int) *Handlers {
|
|
|
|
return &Handlers{
|
|
|
|
icecastUrl: icecastUrl,
|
|
|
|
icecastPlaylistPath: icecastPlaylistPath,
|
|
|
|
songListLen: songListLen,
|
|
|
|
filelistPath: filelistPath}
|
2022-03-08 01:17:24 +04:00
|
|
|
}
|
|
|
|
|
2023-02-07 02:27:30 +04:00
|
|
|
func (h *Handlers) Index(w http.ResponseWriter, r *http.Request) {
|
2023-08-06 03:17:18 +04:00
|
|
|
status, err := radio.IcecastGetStatus(h.icecastUrl)
|
2022-03-08 01:17:24 +04:00
|
|
|
if err != nil {
|
2023-02-07 02:27:30 +04:00
|
|
|
log.Println("failed to get Icecast status:", err)
|
2022-03-08 01:17:24 +04:00
|
|
|
}
|
|
|
|
|
2023-08-06 03:17:18 +04:00
|
|
|
songs, err := radio.IcecastLastSongs(h.icecastPlaylistPath)
|
2022-03-30 20:20:30 +04:00
|
|
|
if err != nil {
|
2023-02-07 02:27:30 +04:00
|
|
|
log.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-06 03:17:18 +04:00
|
|
|
web.Index(utils.MainSite(r.Host), h.songListLen, status, &songs, r, w)
|
2022-03-08 01:17:24 +04:00
|
|
|
}
|
|
|
|
|
2023-02-07 02:27:30 +04:00
|
|
|
func (h *Handlers) Status(w http.ResponseWriter, r *http.Request) {
|
2023-08-06 03:17:18 +04:00
|
|
|
status, err := radio.IcecastGetStatus(h.icecastUrl)
|
2022-03-08 01:17:24 +04:00
|
|
|
if err != nil {
|
2023-02-07 02:27:30 +04:00
|
|
|
log.Println("cannot retrieve Icecast status:", err)
|
2022-03-30 19:13:55 +04:00
|
|
|
http.Error(w, "cannot retrieve Icecast status", http.StatusInternalServerError)
|
|
|
|
return
|
2022-03-08 01:17:24 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-02-07 02:27:30 +04:00
|
|
|
func (h *Handlers) LastSong(w http.ResponseWriter, r *http.Request) {
|
2023-08-06 03:17:18 +04:00
|
|
|
song, err := radio.IcecastLastSong(h.icecastPlaylistPath)
|
2022-03-30 20:20:30 +04:00
|
|
|
if err != nil {
|
2023-02-07 02:27:30 +04:00
|
|
|
log.Println("cannot retrieve last songs:", err)
|
2022-03-30 20:20:30 +04:00
|
|
|
}
|
|
|
|
|
2023-03-12 21:32:29 +04:00
|
|
|
if song == nil {
|
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
|
|
|
}
|
|
|
|
|
2023-02-07 02:27:30 +04:00
|
|
|
func (h *Handlers) Playlist(w http.ResponseWriter, _ *http.Request) {
|
2023-02-23 17:49:01 +04:00
|
|
|
w.Header().Add("Content-Disposition", "attachment; filename=\"radio.arav.su.m3u\"")
|
2023-07-22 22:58:42 +04:00
|
|
|
fc, _ := web.AssetsGetFile("playlist.m3u")
|
2022-03-31 18:34:33 +04:00
|
|
|
w.Write(fc)
|
2022-03-08 01:17:24 +04:00
|
|
|
}
|
2023-02-19 21:18:36 +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")
|
2023-08-06 03:17:18 +04:00
|
|
|
data, _ := os.ReadFile(h.filelistPath)
|
2023-02-19 21:18:36 +04:00
|
|
|
w.Write(data)
|
|
|
|
}
|
2023-02-19 21:22:56 +04:00
|
|
|
|
2023-05-21 22:42:17 +04:00
|
|
|
func RobotsTxt(w http.ResponseWriter, _ *http.Request) {
|
2023-02-19 21:22:56 +04:00
|
|
|
w.Header().Add("Content-Disposition", "attachment; filename=\"robots.txt\"")
|
|
|
|
w.Write([]byte("User-agent: *\nDisallow: /assets/\nDisallow: /live/"))
|
|
|
|
}
|
2023-05-21 22:35:37 +04:00
|
|
|
|
|
|
|
func Favicon(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data, _ := web.AssetsGetFile("img/favicon.svg")
|
|
|
|
w.Write(data)
|
|
|
|
}
|
2023-05-21 22:44:09 +04:00
|
|
|
|
|
|
|
func SitemapXML(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data, _ := web.AssetsGetFile("sitemap.xml")
|
|
|
|
w.Write(data)
|
|
|
|
}
|