@@ -10,13 +10,14 @@ import (
10
10
)
11
11
12
12
const defaultStaticFilePath = `./static`
13
- const defaultPathFileNotFound = "/404.html"
13
+ const indexHTML = "/index.html"
14
+ const htmlExtension = ".html"
15
+ const rootPath = "/"
14
16
15
17
func main () {
16
18
app := gofr .New ()
17
19
18
20
staticFilePath := app .Config .GetOrDefault ("STATIC_DIR_PATH" , defaultStaticFilePath )
19
- fileNotFoundPath := app .Config .GetOrDefault ("FILE_NOT_FOUND_PATH" , defaultPathFileNotFound )
20
21
21
22
app .UseMiddleware (func (_ http.Handler ) http.Handler {
22
23
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
@@ -25,20 +26,17 @@ func main() {
25
26
// check if the path has a file extension
26
27
ok , _ := regexp .MatchString (`\.\S+$` , filePath )
27
28
28
- if r .URL .Path == "/" {
29
- filePath = "/index.html"
29
+ if r .URL .Path == rootPath {
30
+ filePath += indexHTML
30
31
} else if ! ok {
31
32
if stat , err := os .Stat (filePath ); err == nil && stat .IsDir () {
32
- filePath = filepath . Join ( r . URL . Path , "/index.html" )
33
+ filePath += indexHTML
33
34
} else {
34
- r . URL . Path += ".html"
35
+ filePath += htmlExtension
35
36
}
36
37
}
37
38
38
- filePath = filepath .Join (staticFilePath , r .URL .Path )
39
39
if _ , err := os .Stat (filePath ); os .IsNotExist (err ) {
40
- r .URL .Path = fileNotFoundPath
41
-
42
40
w .WriteHeader (http .StatusNotFound )
43
41
44
42
filePath = filepath .Join (staticFilePath , "404.html" )
0 commit comments