Skip to content

Commit 1b78f0f

Browse files
committed
fix: serve use .html file as fallback
1 parent 66d10c5 commit 1b78f0f

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

tools/serve.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,28 @@ package main
33
import (
44
"fmt"
55
"net/http"
6+
"os"
67
)
78

9+
type HTMLDir struct {
10+
d http.Dir
11+
}
12+
13+
func (d HTMLDir) Open(name string) (http.File, error) {
14+
f, err := d.d.Open(name)
15+
if os.IsNotExist(err) {
16+
if f, err := d.d.Open(name + ".html"); err == nil {
17+
return f, nil
18+
}
19+
}
20+
return f, err
21+
}
22+
823
func main() {
924
port := "8000"
1025
publicDir := "public"
1126
fmt.Printf("Serving Go by Example at http://127.0.0.1:%s\n", port)
12-
http.ListenAndServe(":"+port, http.FileServer(http.Dir(publicDir)))
27+
fs := http.FileServer(HTMLDir{http.Dir(publicDir)})
28+
http.Handle("/", http.StripPrefix("/", fs))
29+
http.ListenAndServe(":"+port, nil)
1330
}

0 commit comments

Comments
 (0)