Skip to content

Commit

Permalink
fix: serve use .html file as fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
everyx committed Mar 19, 2022
1 parent 66d10c5 commit 1b78f0f
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion tools/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,28 @@ package main
import (
"fmt"
"net/http"
"os"
)

type HTMLDir struct {
d http.Dir
}

func (d HTMLDir) Open(name string) (http.File, error) {
f, err := d.d.Open(name)
if os.IsNotExist(err) {
if f, err := d.d.Open(name + ".html"); err == nil {
return f, nil
}
}
return f, err
}

func main() {
port := "8000"
publicDir := "public"
fmt.Printf("Serving Go by Example at http://127.0.0.1:%s\n", port)
http.ListenAndServe(":"+port, http.FileServer(http.Dir(publicDir)))
fs := http.FileServer(HTMLDir{http.Dir(publicDir)})
http.Handle("/", http.StripPrefix("/", fs))
http.ListenAndServe(":"+port, nil)
}

0 comments on commit 1b78f0f

Please sign in to comment.