Handle directory

This commit is contained in:
amutake 2014-12-30 05:58:43 +09:00
parent e27714e4cb
commit d5641e1d4f
1 changed files with 10 additions and 7 deletions

17
main.go
View File

@ -10,20 +10,23 @@ import (
func handler(w http.ResponseWriter, r *http.Request) { func handler(w http.ResponseWriter, r *http.Request) {
path := "." + r.URL.Path path := "." + r.URL.Path
fmt.Println(path) fmt.Print(path)
_, err := os.Stat(path) file, err := os.Stat(path)
if err == nil { if err == nil && !file.IsDir() {
// file exists // file exists
fmt.Println(" => " + path)
http.ServeFile(w, r, path) http.ServeFile(w, r, path)
} else { } else {
// file does not exists // file does not exist
index := "index.html" index := "index.html"
_, err := os.Stat(index) file, err := os.Stat(index)
if err == nil { if err == nil && !file.IsDir() {
// index.html exists // index.html exists
fmt.Println(" => " + index)
http.ServeFile(w, r, index) http.ServeFile(w, r, index)
} else { } else {
// index.html does not exists // index.html does not exist
fmt.Println(" => NotFound")
http.NotFound(w, r) http.NotFound(w, r)
} }
} }