-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers-of.go
81 lines (62 loc) · 1.43 KB
/
handlers-of.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
80
81
package web_container_proxy
import (
"fmt"
"log"
"os"
"regexp"
)
type Handlers map[string]*Handler
func handlerOf(uri string, hasCustom404 bool, custom404 string) *Handler {
handler := &Handler{false, false, uri, nil}
isStatic := isLocalPath(uri)
if isStatic && isSingleFile(uri) {
handler.isStatic = true
handler.server = newSingleFileServer(uri)
log.Println("isSingleFile")
} else if isStatic {
handler.isStatic = true
handler.server = newStaticServer(uri, hasCustom404, custom404)
log.Println("newStaticServer")
} else {
handler.isReverseProxy = true
handler.server = ReverseProxyServer(uri)
log.Println("ReverseProxyServer")
}
return handler
}
func handlersOf(options map[string]string) Handlers {
handlers := make(Handlers)
custom404, hasCustom404 := options["*"]
if hasCustom404 {
hasCustom404 = isLocalPath(custom404)
}
if hasCustom404 {
custom404 = fmt.Sprintf("%s/index.html", custom404)
}
for path, uri := range options {
handlers[path] = handlerOf(uri, hasCustom404, custom404)
}
return handlers
}
func isLocalPath(config string) bool {
matches, _ := regexp.MatchString("^/", config)
return matches
}
func isSingleFile(uri string) bool {
f, err := os.Open(uri)
if err != nil {
return false
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return false
}
switch mode := fi.Mode(); {
case mode.IsDir():
return false
case mode.IsRegular():
return true
}
return false
}