forked from PierreZ/goStatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfallback.go
36 lines (31 loc) · 831 Bytes
/
fallback.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
import (
"net/http"
"os"
"path"
)
// fallback opens defaultPath when the underlying fs returns os.ErrNotExist
type fallback struct {
defaultPath string
fs http.FileSystem
}
func OpenDefault(fb fallback, requestPath string) (http.File, error) {
requestPath = path.Dir(requestPath)
defaultFile := requestPath + "/" + fb.defaultPath;
f, err := fb.fs.Open(defaultFile)
if os.IsNotExist(err) && requestPath != "" {
parentPath, _ := path.Split(requestPath)
return OpenDefault(fb, parentPath)
}
return f, err
}
func (fb fallback) Open(requestPath string) (http.File, error) {
f, err := fb.fs.Open(requestPath)
if os.IsNotExist(err) {
if len(fb.defaultPath) == 0 || fb.defaultPath[0] == '/' {
return fb.fs.Open(fb.defaultPath)
}
return OpenDefault(fb, requestPath)
}
return f, err
}