Skip to content

Commit c4f9a82

Browse files
committed
add check if the filepath ends with a directory
1 parent 767da41 commit c4f9a82

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

main.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@ func main() {
1818

1919
app.UseMiddleware(func(_ http.Handler) http.Handler {
2020
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
21-
// check if the path has a file extension
22-
ok, _ := regexp.MatchString(`\.\S+$`, r.URL.Path)
21+
filePath := filepath.Join(staticFilePath, r.URL.Path)
2322

24-
if r.URL.Path != "/" && !ok {
25-
r.URL.Path += ".html"
23+
// check if the path has a file extension
24+
ok, _ := regexp.MatchString(`\.\S+$`, filePath)
25+
26+
if r.URL.Path == "/" {
27+
filePath = "/index.html"
28+
} else if !ok {
29+
if stat, err := os.Stat(filePath); err == nil && stat.IsDir() {
30+
filePath = filepath.Join(r.URL.Path, "/index.html")
31+
} else {
32+
r.URL.Path += ".html"
33+
}
2634
}
2735

28-
filePath := filepath.Join(staticFilePath, r.URL.Path)
36+
filePath = filepath.Join(staticFilePath, r.URL.Path)
2937
if _, err := os.Stat(filePath); os.IsNotExist(err) {
3038
r.URL.Path = "/404.html"
3139

0 commit comments

Comments
 (0)