From ee96af31ad74daef87f881d3c60557243fedc56d Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Thu, 31 Mar 2022 17:01:51 +0400 Subject: [PATCH] Added a robots.txt file and a handler for it. --- cmd/dwelling-radio/main.go | 1 + internal/handlers/handlers.go | 17 +++++++++++++++++ internal/handlers/web/assets/robots.txt | 3 +++ 3 files changed, 21 insertions(+) create mode 100644 internal/handlers/web/assets/robots.txt diff --git a/cmd/dwelling-radio/main.go b/cmd/dwelling-radio/main.go index 5f2cc72..a1a0bed 100644 --- a/cmd/dwelling-radio/main.go +++ b/cmd/dwelling-radio/main.go @@ -57,6 +57,7 @@ func main() { srv.ServeStatic("/assets/*filepath", hand.AssetsFS()) srv.GET("/", hand.Index) + srv.GET("/robots.txt", hand.Robots) srv.GET("/stats", hand.Stats) srv.GET("/lastsong", hand.LastSong) srv.GET("/playlist", hand.Playlist) diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 7cec992..757f530 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -10,6 +10,7 @@ import ( "fmt" "html/template" "io/fs" + "io/ioutil" "net/http" "time" @@ -54,6 +55,22 @@ func (h *RadioHandlers) AssetsFS() http.FileSystem { return http.FS(f) } +func (h *RadioHandlers) Robots(w http.ResponseWriter, r *http.Request) { + f, err := h.AssetsFS().Open("robots.txt") + if err != nil { + w.WriteHeader(http.StatusNotFound) + return + } + defer f.Close() + + fc, err := ioutil.ReadAll(f) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + } + + w.Write(fc) +} + func (h *RadioHandlers) Index(w http.ResponseWriter, r *http.Request) { status, err := radio.IcecastGetStatus(h.conf.Icecast.URL) if err != nil { diff --git a/internal/handlers/web/assets/robots.txt b/internal/handlers/web/assets/robots.txt new file mode 100644 index 0000000..912a23f --- /dev/null +++ b/internal/handlers/web/assets/robots.txt @@ -0,0 +1,3 @@ +User-agent: * +Disallow: /assets/ +Disallow: /live/ \ No newline at end of file