diff --git a/main.go b/main.go index f69ce27..9ac6afe 100644 --- a/main.go +++ b/main.go @@ -4,10 +4,24 @@ import ( "fmt" "log" "net/http" + "os" + "path" ) +func customFileServer(fs http.FileSystem) http.Handler { + fileServer := http.FileServer(fs) + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, err := fs.Open(path.Clean(r.URL.Path)) // Do not allow path traversals. + if os.IsNotExist(err) { + http.ServeFile(w, r, "./static/index.html") + return + } + fileServer.ServeHTTP(w, r) + }) +} + func main() { - fs := http.FileServer(http.Dir("./static")) + fs := customFileServer(http.Dir("./static")) http.Handle("/", fs) fmt.Print("Listening on 4000") err := http.ListenAndServe(":4000", nil)