From 0244f6afd55dd5b575dd88a3f445550a91707417 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sat, 20 Apr 2024 18:54:05 +0400 Subject: [PATCH] A unified ServeAsset() func was introduced. AssetsGetFile() now will panic if a file doesn't exists, because it must not be the case. --- cmd/dwelling-radio/main.go | 8 ++++---- internal/http/handlers.go | 27 ++++++++++----------------- web/assets/robots.txt | 3 +++ web/web.go | 9 +++++++-- 4 files changed, 24 insertions(+), 23 deletions(-) create mode 100644 web/assets/robots.txt diff --git a/cmd/dwelling-radio/main.go b/cmd/dwelling-radio/main.go index b53c0f4..5ae1fb7 100644 --- a/cmd/dwelling-radio/main.go +++ b/cmd/dwelling-radio/main.go @@ -57,12 +57,12 @@ func main() { r.Handler(http.MethodGet, "/", hand.Index) - r.Handler(http.MethodGet, "/playlist", hand.Playlist) + r.Handler(http.MethodGet, "/playlist", ihttp.ServeAsset("playlist.m3u", "", "radio.arav.su.m3u")) r.Handler(http.MethodGet, "/filelist", hand.Filelist) - r.Handler(http.MethodGet, "/robots.txt", ihttp.RobotsTxt) - r.Handler(http.MethodGet, "/sitemap.xml", ihttp.SitemapXML) - r.Handler(http.MethodGet, "/favicon.svg", ihttp.Favicon) + r.Handler(http.MethodGet, "/robots.txt", ihttp.ServeAsset("robots.txt", "text/plain", "")) + r.Handler(http.MethodGet, "/sitemap.xml", ihttp.ServeAsset("sitemap.xml", "application/xml", "")) + r.Handler(http.MethodGet, "/favicon.svg", ihttp.ServeAsset("favicon.svg", "image/svg", "")) r.ServeStatic("/assets/*filepath", web.Assets()) diff --git a/internal/http/handlers.go b/internal/http/handlers.go index d5fd817..fe6144d 100644 --- a/internal/http/handlers.go +++ b/internal/http/handlers.go @@ -27,29 +27,22 @@ func (h *Handlers) Index(w http.ResponseWriter, r *http.Request) { web.Index(utils.MainSite(r.Host), h.songList, h.listeners, h.mostLSong.Get(), r, w) } -func (h *Handlers) Playlist(w http.ResponseWriter, _ *http.Request) { - w.Header().Add("Content-Disposition", "attachment; filename=\"radio.arav.su.m3u\"") - fc, _ := web.AssetsGetFile("playlist.m3u") - w.Write(fc) -} - func (h *Handlers) Filelist(w http.ResponseWriter, _ *http.Request) { w.Header().Add("Content-Type", "text/html") data, _ := os.ReadFile(h.filelistPath) w.Write(data) } -func RobotsTxt(w http.ResponseWriter, _ *http.Request) { - w.Header().Add("Content-Disposition", "attachment; filename=\"robots.txt\"") - w.Write([]byte("User-agent: *\nDisallow: /assets/\nDisallow: /live/")) -} +func ServeAsset(path, mime, attachement string) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + if mime != "" { + w.Header().Add("Content-Type", mime) + } -func Favicon(w http.ResponseWriter, r *http.Request) { - data, _ := web.AssetsGetFile("img/favicon.svg") - w.Write(data) -} + if attachement != "" { + w.Header().Add("Content-Disposition", "attachment; filename=\""+path+"\"") + } -func SitemapXML(w http.ResponseWriter, r *http.Request) { - data, _ := web.AssetsGetFile("sitemap.xml") - w.Write(data) + w.Write(web.AssetsGetFile(path)) + } } diff --git a/web/assets/robots.txt b/web/assets/robots.txt new file mode 100644 index 0000000..912a23f --- /dev/null +++ b/web/assets/robots.txt @@ -0,0 +1,3 @@ +User-agent: * +Disallow: /assets/ +Disallow: /live/ \ No newline at end of file diff --git a/web/web.go b/web/web.go index 2a60d57..4a1d8cd 100644 --- a/web/web.go +++ b/web/web.go @@ -17,6 +17,11 @@ func Assets() http.FileSystem { return http.FS(f) } -func AssetsGetFile(path string) ([]byte, error) { - return assetsDir.ReadFile("assets/" + path) +func AssetsGetFile(path string) []byte { + data, err := assetsDir.ReadFile("assets/" + path) + if err != nil { + panic(err) + } + + return data }