// FROM https://gist.github.com/lummie/91cd1c18b2e32fa9f316862221a6fd5c

package webserver

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)
	})
}
