strimertul/static-ex.go

42 lines
782 B
Go

// FROM https://gist.github.com/lummie/91cd1c18b2e32fa9f316862221a6fd5c
package main
import (
"net/http"
"os"
"path"
"strings"
)
func FileServerWithDefault(root http.FileSystem) http.Handler {
fs := http.FileServer(root)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
//make sure the url path starts with /
upath := r.URL.Path
if !strings.HasPrefix(upath, "/") {
upath = "/" + upath
r.URL.Path = upath
}
upath = path.Clean(upath)
// attempt to open the file via the http.FileSystem
f, err := root.Open(upath)
if err != nil {
if os.IsNotExist(err) {
// Revert to homepage
r.URL.Path = "/"
}
}
// close if successfully opened
if err == nil {
f.Close()
}
// default serve
fs.ServeHTTP(w, r)
})
}