From d5641e1d4f08e95e3dad450c5dbbb092ef0500ae Mon Sep 17 00:00:00 2001 From: amutake Date: Tue, 30 Dec 2014 05:58:43 +0900 Subject: [PATCH] Handle directory --- main.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index aad3f1b..6dd3656 100644 --- a/main.go +++ b/main.go @@ -10,20 +10,23 @@ import ( func handler(w http.ResponseWriter, r *http.Request) { path := "." + r.URL.Path - fmt.Println(path) - _, err := os.Stat(path) - if err == nil { + fmt.Print(path) + file, err := os.Stat(path) + if err == nil && !file.IsDir() { // file exists + fmt.Println(" => " + path) http.ServeFile(w, r, path) } else { - // file does not exists + // file does not exist index := "index.html" - _, err := os.Stat(index) - if err == nil { + file, err := os.Stat(index) + if err == nil && !file.IsDir() { // index.html exists + fmt.Println(" => " + index) http.ServeFile(w, r, index) } else { - // index.html does not exists + // index.html does not exist + fmt.Println(" => NotFound") http.NotFound(w, r) } }