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