-
Notifications
You must be signed in to change notification settings - Fork 0
/
declaration.go
202 lines (173 loc) · 3.48 KB
/
declaration.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package vintage
import (
"io"
"net"
"time"
"github.com/pkg/errors"
)
type Backend struct {
Name string
IsDefault bool
Port string
Host string
SSL bool
AlwaysUseHostHeader bool
ConnectTimeout time.Duration
FirstByteTimeout time.Duration
BetweenBytesTimeout time.Duration
Director *Director
}
func NewBackend(name string, opts ...BackendOption) *Backend {
b := &Backend{
Name: name,
ConnectTimeout: time.Second,
FirstByteTimeout: 15 * time.Second,
BetweenBytesTimeout: 10 * time.Second,
}
for i := range opts {
opts[i](b)
}
return b
}
func (b *Backend) Backend(ident RequestIdentity) string {
if b.Director != nil {
return b.Director.Backend(ident)
}
return b.Name
}
type BackendOption func(b *Backend)
func BackendDefault() BackendOption {
return func(b *Backend) {
b.IsDefault = true
}
}
func BackendPort(port string) BackendOption {
return func(b *Backend) {
b.Port = port
}
}
func BackendHost(host string) BackendOption {
return func(b *Backend) {
b.Host = host
}
}
func BackendSSL(ssl bool) BackendOption {
return func(b *Backend) {
b.SSL = ssl
}
}
func BackendConnectTimeout(t time.Duration) BackendOption {
return func(b *Backend) {
b.ConnectTimeout = t
}
}
func BackendFirstByteTimeout(t time.Duration) BackendOption {
return func(b *Backend) {
b.FirstByteTimeout = t
}
}
func BackendBetweenBytesTimeout(t time.Duration) BackendOption {
return func(b *Backend) {
b.BetweenBytesTimeout = t
}
}
func BackendAlwaysUseHostHeader(v bool) BackendOption {
return func(b *Backend) {
b.AlwaysUseHostHeader = v
}
}
type Acl struct {
Name string
CIDRs []struct {
IpNet *net.IPNet
Inverse bool
}
}
func NewAcl(name string, opts ...AclOption) *Acl {
a := &Acl{
Name: name,
}
for i := range opts {
opts[i](a)
}
return a
}
func (a *Acl) Match(ip net.IP) bool {
for _, entry := range a.CIDRs {
contains := entry.IpNet.Contains(ip)
if contains {
return true
}
if entry.Inverse {
return true
}
}
return false
}
type AclOption func(a *Acl)
func AclEntry(cidr string, inverse bool) AclOption {
return func(a *Acl) {
_, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
panic(err)
}
a.CIDRs = append(a.CIDRs, struct {
IpNet *net.IPNet
Inverse bool
}{
IpNet: ipnet,
Inverse: inverse,
})
}
}
type Table struct {
Name string
Type string
Items map[string]any
isEdgeDictionary bool
}
type TableOption func(t *Table)
func TableItem(name string, value any) TableOption {
return func(t *Table) {
t.Items[name] = value
}
}
func EdgeDictionary() TableOption {
return func(t *Table) {
t.isEdgeDictionary = true
}
}
func NewTable(name, itemType string, items ...TableOption) *Table {
t := &Table{
Name: name,
Type: itemType,
Items: make(map[string]any),
}
for i := range items {
items[i](t)
}
return t
}
func (t *Table) IsEdgeDictionary() bool {
return t.isEdgeDictionary
}
type LoggingEndpoint struct {
Name string
initiator LoggerInitiator
}
func NewLoggingEndpoint(name string, initiator LoggerInitiator) *LoggingEndpoint {
return &LoggingEndpoint{
Name: name,
initiator: initiator,
}
}
func (l *LoggingEndpoint) Write(message string) error {
w, err := l.initiator(l.Name)
if err != nil {
return errors.WithStack(err)
}
if _, err := io.WriteString(w, message); err != nil {
return errors.WithStack(err)
}
return nil
}