1
0
Fork 0
mirror of https://git.sr.ht/~ashkeel/strimertul synced 2024-09-18 01:50:50 +00:00
strimertul/http/static-ex.go
2022-11-30 19:15:47 +01:00

40 lines
775 B
Go

// FROM https://gist.github.com/lummie/91cd1c18b2e32fa9f316862221a6fd5c
package http
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 = "/"
}
} else {
// close if successfully opened
_ = f.Close()
}
// default serve
fs.ServeHTTP(w, r)
})
}