Handle directory
This commit is contained in:
parent
e27714e4cb
commit
d5641e1d4f
17
main.go
17
main.go
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue