-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmatchers.go
82 lines (72 loc) · 2.19 KB
/
matchers.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
package caddy_docker_upstreams
import (
"net/url"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"go.uber.org/zap"
)
const (
LabelMatchProtocol = "com.caddyserver.http.matchers.protocol"
LabelMatchHost = "com.caddyserver.http.matchers.host"
LabelMatchMethod = "com.caddyserver.http.matchers.method"
LabelMatchPath = "com.caddyserver.http.matchers.path"
LabelMatchQuery = "com.caddyserver.http.matchers.query"
LabelMatchExpression = "com.caddyserver.http.matchers.expression"
)
var producers = map[string]func(string) (caddyhttp.RequestMatcher, error){
LabelMatchProtocol: func(value string) (caddyhttp.RequestMatcher, error) {
matcher := caddyhttp.MatchProtocol(value)
return &matcher, nil
},
LabelMatchHost: func(value string) (caddyhttp.RequestMatcher, error) {
return &caddyhttp.MatchHost{value}, nil
},
LabelMatchMethod: func(value string) (caddyhttp.RequestMatcher, error) {
return &caddyhttp.MatchMethod{value}, nil
},
LabelMatchPath: func(value string) (caddyhttp.RequestMatcher, error) {
return &caddyhttp.MatchPath{value}, nil
},
LabelMatchQuery: func(value string) (caddyhttp.RequestMatcher, error) {
query, err := url.ParseQuery(value)
if err != nil {
return nil, err
}
matcher := caddyhttp.MatchQuery(query)
return &matcher, nil
},
LabelMatchExpression: func(value string) (caddyhttp.RequestMatcher, error) {
return &caddyhttp.MatchExpression{Expr: value}, nil
},
}
func buildMatchers(ctx caddy.Context, labels map[string]string) caddyhttp.MatcherSet {
var matchers caddyhttp.MatcherSet
for key, producer := range producers {
value, ok := labels[key]
if !ok {
continue
}
matcher, err := producer(value)
if err != nil {
ctx.Logger().Error("unable to load matcher",
zap.String("key", key),
zap.String("value", value),
zap.Error(err),
)
continue
}
if prov, ok := matcher.(caddy.Provisioner); ok {
err = prov.Provision(ctx)
if err != nil {
ctx.Logger().Error("unable to provision matcher",
zap.String("key", key),
zap.String("value", value),
zap.Error(err),
)
continue
}
}
matchers = append(matchers, matcher)
}
return matchers
}