Serve robots.txt from root (http://server/robots.txt)

This commit is contained in:
Deluan
2020-10-02 10:15:19 -04:00
parent deef8e162d
commit 8dfc259857
5 changed files with 91 additions and 4 deletions
+15 -1
View File
@@ -3,13 +3,14 @@ package server
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/deluan/navidrome/log"
"github.com/go-chi/chi/middleware"
)
func RequestLogger(next http.Handler) http.Handler {
func requestLogger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
scheme := "http"
if r.TLS != nil {
@@ -44,3 +45,16 @@ func RequestLogger(next http.Handler) http.Handler {
}
})
}
func robotsTXT(fs http.FileSystem) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/robots.txt") {
r.URL.Path = "/robots.txt"
http.FileServer(fs).ServeHTTP(w, r)
} else {
next.ServeHTTP(w, r)
}
})
}
}