forked from simonmittag/j8a
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.go
87 lines (76 loc) · 2.15 KB
/
route.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
82
83
84
85
86
87
package jabba
import (
"github.com/rs/zerolog/log"
"net/http"
"regexp"
"time"
)
//AboutJabba special Route alias for internal endpoint
const AboutJabba string = "aboutJabba"
type Routes []Route
func (s Routes) Len() int {
return len(s)
}
func (s Routes) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s Routes) Less(i, j int) bool {
return len(s[i].Path) > len(s[j].Path)
}
//Route maps a Path to an upstream resource
type Route struct {
Path string
Regex *regexp.Regexp
Resource string
Policy string
}
func (route Route) matchURI(request *http.Request) bool {
matched := false
if route.Regex != nil {
matched = route.Regex.MatchString(request.RequestURI)
} else {
matched, _ = regexp.MatchString("^"+route.Path, request.RequestURI)
}
return matched
}
// maps a route to a URL. Returns the URL, the name of the mapped policy and whether mapping was successful
func (route Route) mapURL(proxy *Proxy) (*URL, string, bool) {
var policy Policy
var policyLabel string
if len(route.Policy) > 0 {
policy = Runner.Policies[route.Policy]
policyLabel = policy.resolveLabel()
}
resource := Runner.Resources[route.Resource]
//if a policy exists, we match resources with a label. TODO: this should be an interface
if len(route.Policy) > 0 {
for _, resourceMapping := range resource {
for _, resourceLabel := range resourceMapping.Labels {
if policyLabel == resourceLabel {
log.Trace().
Str("routePath", route.Path).
Str("upstream", resourceMapping.URL.String()).
Str("label", resourceLabel).
Str("policy", route.Policy).
Str(XRequestID, proxy.XRequestID).
Int64("dwnElapsedMicros", time.Since(proxy.Dwn.startDate).Microseconds()).
Msg("upstream route mapped")
return &resourceMapping.URL, policyLabel, true
}
}
}
} else {
log.Trace().
Str("routePath", route.Path).
Str("policy", "default").
Str(XRequestID, proxy.XRequestID).
Str("upstream", resource[0].URL.String()).
Msg("route mapped")
return &resource[0].URL, "default", true
}
log.Trace().
Str("routePath", route.Path).
Str(XRequestID, proxy.XRequestID).
Msg("route not mapped")
return nil, "", false
}