From 0baef712b28f7450b4202790b3d2cbb7faef6797 Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Sun, 12 Dec 2021 09:08:46 -0700 Subject: [PATCH] Handle case where we have an index.html that isn't included in the url path. Fixes #14 --- main.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 670dfa9..9b294e5 100644 --- a/main.go +++ b/main.go @@ -372,6 +372,16 @@ func main() { } if len(entries) > 0 { + if r.URL.Path == "/" { + // If we have entries, and are serving up /, check for + // index.html and redirect to that if it exists. We redirect + // because net/http handles index.html magically for FileServer + _, fErr := os.Stat(filepath.Clean(path.Join(userPath, "index.html"))) + if !os.IsNotExist(fErr) { + http.Redirect(w, r, "/index.html", 301) + return + } + } handler.fs.ServeHTTP(w, r) } else { l := Landing{ @@ -415,4 +425,4 @@ func main() { log.Printf("Listening for HTTP on 'http://%s'", listen) log.Fatalln(s.Serve(lis)) } -} \ No newline at end of file +}