-
Notifications
You must be signed in to change notification settings - Fork 2
/
gateway_api.go
88 lines (75 loc) · 1.85 KB
/
gateway_api.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
88
package gw_policies_playground
type void struct{}
var sentinel void
type GatewayClass[T Policy] struct {
name string
gateways map[*Gateway[T]]void
policies []PolicySpec[T]
}
func (gwc *GatewayClass[T]) CreateGateway(name string) *Gateway[T] {
gw := &Gateway[T]{
parent: gwc,
name: name,
routes: make(map[*HttpRoute[T]]void),
}
gwc.gateways[gw] = sentinel
return gw
}
type Gateway[T Policy] struct {
parent *GatewayClass[T]
name string
routes map[*HttpRoute[T]]void
policies []PolicySpec[T]
}
func (gw *Gateway[T]) CreateRoute(name string) *HttpRoute[T] {
r := &HttpRoute[T]{
parent: gw,
name: name,
}
gw.routes[r] = sentinel
return r
}
func (gw *Gateway[T]) AddPolicy(policy PolicySpec[T]) {
gw.policies = append(gw.policies, policy)
}
type HttpRoute[T Policy] struct {
parent *Gateway[T]
name string
policies []PolicySpec[T]
}
func (r *HttpRoute[T]) AddPolicy(policy PolicySpec[T]) {
r.policies = append(r.policies, policy)
}
func (r *HttpRoute[T]) MergedPolicies(merger func(T, T) T) T {
var policies []T
for _, policy := range r.policies {
policies = append(policies, policy.defaults)
policies = append([]T{policy.overrides}, policies...)
}
for _, policy := range r.parent.policies {
policies = append(policies, policy.defaults)
policies = append([]T{policy.overrides}, policies...)
}
for _, policy := range r.parent.parent.policies {
policies = append(policies, policy.defaults)
policies = append([]T{policy.overrides}, policies...)
}
result := policies[0]
for _, policy := range policies[1:] {
result = merger(result, policy)
}
return result
}
type PolicySpec[T Policy] struct {
name string
defaults T
overrides T
}
type Policy interface {
}
func NewGatewayClass[T Policy](name string) GatewayClass[T] {
return GatewayClass[T]{
name: name,
gateways: make(map[*Gateway[T]]void),
}
}