-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatic.go
60 lines (48 loc) · 1.1 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
package s
import (
"net/http"
"os"
"strings"
"time"
)
var statics = make(map[string]*string)
func Static(path, rootPath string) {
if rootPath[0] != '/' {
pos := strings.LastIndexByte(os.Args[0], '/')
if pos > 0 {
rootPath = os.Args[0][0:pos+1] + rootPath
}
}
statics[path] = &rootPath
}
func processStatic(requestPath string, request *http.Request, response *Response, headers *map[string]string, startTime *time.Time) bool {
if len(statics) == 0 {
return false
}
rootPath := statics[requestPath]
if rootPath == nil {
for p1, p2 := range statics {
if strings.HasPrefix(requestPath, p1) {
rootPath = p2
break
}
}
}
if rootPath == nil {
return false
}
filePath := *rootPath + requestPath
if strings.HasSuffix(filePath, "/") {
filePath += "index.html"
}
_, err := os.Stat(filePath)
if err != nil {
return false
}
if strings.HasSuffix(filePath, "/index.html") {
filePath = filePath[len(filePath)-11:]
}
http.ServeFile(response, request, *rootPath+requestPath)
writeLog("STATIC", nil, 0, request, response, nil, headers, startTime, 0, nil)
return true
}