-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathstatic.go
79 lines (64 loc) · 1.74 KB
/
static.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package neo
import (
"errors"
"os"
"path/filepath"
"strings"
)
type Static struct {
serving map[string]string
}
func (s *Static) init() {
s.serving = map[string]string{}
}
// If path exists, and it is not directory it can be served.
// Othervise path cannot be served.
func (s *Static) canBeServed(path string) bool {
stat, err := os.Stat(path)
if err != nil {
log.Debugf("Error while calling os.Stat for path %s. Error: %s", path, err.Error())
} else {
if !stat.IsDir() {
return true
}
}
return false
}
// Trying to find file from passed url.
// If you have called ``app.Assert("/some", "./dir")``, then if url is ``/some/path/file.txt``,
// then method will look for file at ``./dir/path/file.txt``.
func (s *Static) match(url string) (string, error) {
log.Debug("trying to match static url: " + url)
if url == "/" {
url = "/index.html"
}
for k, v := range s.serving {
if strings.Index(url, k) == 0 {
log.Debugf("replacing %s with %s", url, k)
file := strings.Replace(url, k, "", 1)
if k == "/" {
file = k + file
}
path := v + file
log.Debug("found possible result. Path: " + path)
if s.canBeServed(path) {
return path, nil
} else if s.canBeServed(path + "/index.html") {
return path + "/index.html", nil
}
}
}
return "", errors.New("cannot match " + url + " in static")
}
// Serving static files from some directory.
// If provided url is for example ``/assets``,
// then all requests starting with ``/assets`` will be served from directory
// provided by ``path`` parameter.
func (s *Static) Serve(url string, path string) {
path, err := filepath.Abs(path)
if err != nil {
panic("Cannot make absolute path for " + path)
}
log.Debug("Serving static at " + path)
s.serving[url] = path
}