A unified ServeAsset() func was introduced. AssetsGetFile() now will panic if a file doesn't exists, because it must not be the case.
This commit is contained in:
parent
5575408560
commit
0244f6afd5
@ -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())
|
||||
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
3
web/assets/robots.txt
Normal file
3
web/assets/robots.txt
Normal file
@ -0,0 +1,3 @@
|
||||
User-agent: *
|
||||
Disallow: /assets/
|
||||
Disallow: /live/
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user