lpar / gzipped

Replacement for golang http.FileServer which supports precompressed static assets.
BSD 3-Clause "New" or "Revised" License
94 stars 15 forks source link

Example serve index.html for ./ doesn't work in go..1.13 #4

Closed nathanhack closed 4 years ago

nathanhack commented 4 years ago

I don't know if it happens in older versions but in go1.13. The example middlewrapper ends up in a loop of redirecting.

The middleware basically adds "index.html" to "/" which is fine but it will eventually be sent to http.serveFile() which does the following:

if strings.HasSuffix(r.URL.Path, indexPage) {
        localRedirect(w, r, "./")
        return
}

That localRedirect sends it back to the beginning, and it just loops over and over.

I was able to get it working by serving the file directly in the middleware:

func handleIndexPage(h http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        if strings.HasSuffix(r.URL.Path, "/") {
            http.ServeFile(w, r, "./index.html")
            return
        }
        h.ServeHTTP(w, r)
    })
}
lpar commented 4 years ago

I've fixed the README to give separate working examples, both for full directory browsing and for just index.html handling.