package handlers import ( "io/fs" "net/http" "strings" "sneak.berlin/go/dnswatcher/static" ) // HandleStatic returns the handler for the embedded static assets. It // is mounted under /s, with that prefix stripped from the path. A // request that names a directory answers 404: http.FileServer would // answer it with a listing of the directory, or with a redirect to // that listing when the trailing slash is missing. func (h *Handlers) HandleStatic() http.HandlerFunc { files := http.FileServer(http.FS(static.Static)) return func( writer http.ResponseWriter, request *http.Request, ) { // fs.Stat refuses a name that is empty or ends in a slash, // so /s/ and /s/css/ answer 404 here along with /s/css. name := strings.TrimPrefix(request.URL.Path, "/") info, err := fs.Stat(static.Static, name) if err != nil || info.IsDir() { http.NotFound(writer, request) return } files.ServeHTTP(writer, request) } }