-
Notifications
You must be signed in to change notification settings - Fork 217
/
options.go
266 lines (243 loc) · 7.28 KB
/
options.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package http
import (
"fmt"
"net"
nethttp "net/http"
"net/url"
"strings"
"time"
)
// Option is the function signature required to be considered an http.Option.
type Option func(*Transport) error
// WithTarget sets the outbound recipient of cloudevents when using an HTTP
// request.
func WithTarget(targetUrl string) Option {
return func(t *Transport) error {
if t == nil {
return fmt.Errorf("http target option can not set nil transport")
}
targetUrl = strings.TrimSpace(targetUrl)
if targetUrl != "" {
var err error
var target *url.URL
target, err = url.Parse(targetUrl)
if err != nil {
return fmt.Errorf("http target option failed to parse target url: %s", err.Error())
}
if t.Req == nil {
t.Req = &nethttp.Request{
Method: nethttp.MethodPost,
}
}
t.Req.URL = target
return nil
}
return fmt.Errorf("http target option was empty string")
}
}
// WithMethod sets the outbound recipient of cloudevents when using an HTTP
// request.
func WithMethod(method string) Option {
return func(t *Transport) error {
if t == nil {
return fmt.Errorf("http method option can not set nil transport")
}
method = strings.TrimSpace(method)
if method != "" {
if t.Req == nil {
t.Req = &nethttp.Request{}
}
t.Req.Method = method
return nil
}
return fmt.Errorf("http method option was empty string")
}
}
// WithHeader sets an additional default outbound header for all cloudevents
// when using an HTTP request.
func WithHeader(key, value string) Option {
return func(t *Transport) error {
if t == nil {
return fmt.Errorf("http header option can not set nil transport")
}
key = strings.TrimSpace(key)
if key != "" {
if t.Req == nil {
t.Req = &nethttp.Request{}
}
if t.Req.Header == nil {
t.Req.Header = nethttp.Header{}
}
t.Req.Header.Add(key, value)
return nil
}
return fmt.Errorf("http header option was empty string")
}
}
// WithShutdownTimeout sets the shutdown timeout when the http server is being shutdown.
func WithShutdownTimeout(timeout time.Duration) Option {
return func(t *Transport) error {
if t == nil {
return fmt.Errorf("http shutdown timeout option can not set nil transport")
}
t.ShutdownTimeout = &timeout
return nil
}
}
// WithEncoding sets the encoding for clients with HTTP transports.
func WithEncoding(encoding Encoding) Option {
return func(t *Transport) error {
if t == nil {
return fmt.Errorf("http encoding option can not set nil transport")
}
t.Encoding = encoding
return nil
}
}
// WithDefaultEncodingSelector sets the encoding selection strategy for
// default encoding selections based on Event.
func WithDefaultEncodingSelector(fn EncodingSelector) Option {
return func(t *Transport) error {
if t == nil {
return fmt.Errorf("http default encoding selector option can not set nil transport")
}
if fn != nil {
t.DefaultEncodingSelectionFn = fn
return nil
}
return fmt.Errorf("http fn for DefaultEncodingSelector was nil")
}
}
// WithContextBasedEncoding sets the encoding selection strategy for
// default encoding selections based context and then on Event, the encoded
// event will be the given version in the encoding specified by the given
// context, or Binary if not set.
func WithContextBasedEncoding() Option {
return func(t *Transport) error {
if t == nil {
return fmt.Errorf("http context based encoding option can not set nil transport")
}
t.DefaultEncodingSelectionFn = ContextBasedEncodingSelectionStrategy
return nil
}
}
// WithBinaryEncoding sets the encoding selection strategy for
// default encoding selections based on Event, the encoded event will be the
// given version in Binary form.
func WithBinaryEncoding() Option {
return func(t *Transport) error {
if t == nil {
return fmt.Errorf("http binary encoding option can not set nil transport")
}
t.DefaultEncodingSelectionFn = DefaultBinaryEncodingSelectionStrategy
return nil
}
}
// WithStructuredEncoding sets the encoding selection strategy for
// default encoding selections based on Event, the encoded event will be the
// given version in Structured form.
func WithStructuredEncoding() Option {
return func(t *Transport) error {
if t == nil {
return fmt.Errorf("http structured encoding option can not set nil transport")
}
t.DefaultEncodingSelectionFn = DefaultStructuredEncodingSelectionStrategy
return nil
}
}
func checkListen(t *Transport, prefix string) error {
switch {
case t.Port != nil:
return fmt.Errorf("%v port already set", prefix)
case t.listener != nil:
return fmt.Errorf("%v listener already set", prefix)
}
return nil
}
// WithPort sets the listening port for StartReceiver.
// Only one of WithListener or WithPort is allowed.
func WithPort(port int) Option {
return func(t *Transport) error {
if t == nil {
return fmt.Errorf("http port option can not set nil transport")
}
if port < 0 {
return fmt.Errorf("http port option was given an invalid port: %d", port)
}
if err := checkListen(t, "http port option"); err != nil {
return err
}
t.setPort(port)
return nil
}
}
// WithListener sets the listener for StartReceiver.
// Only one of WithListener or WithPort is allowed.
func WithListener(l net.Listener) Option {
return func(t *Transport) error {
if t == nil {
return fmt.Errorf("http listener option can not set nil transport")
}
if err := checkListen(t, "http port option"); err != nil {
return err
}
t.listener = l
_, err := t.listen()
return err
}
}
// WithPath sets the path to receive cloudevents on for HTTP transports.
func WithPath(path string) Option {
return func(t *Transport) error {
if t == nil {
return fmt.Errorf("http path option can not set nil transport")
}
path = strings.TrimSpace(path)
if len(path) == 0 {
return fmt.Errorf("http path option was given an invalid path: %q", path)
}
t.Path = path
return nil
}
}
// Middleware is a function that takes an existing http.Handler and wraps it in middleware,
// returning the wrapped http.Handler.
type Middleware func(next nethttp.Handler) nethttp.Handler
// WithMiddleware adds an HTTP middleware to the transport. It may be specified multiple times.
// Middleware is applied to everything before it. For example
// `NewClient(WithMiddleware(foo), WithMiddleware(bar))` would result in `bar(foo(original))`.
func WithMiddleware(middleware Middleware) Option {
return func(t *Transport) error {
if t == nil {
return fmt.Errorf("http middleware option can not set nil transport")
}
t.middleware = append(t.middleware, middleware)
return nil
}
}
// WithLongPollTarget sets the receivers URL to perform long polling after
// StartReceiver is called.
func WithLongPollTarget(targetUrl string) Option {
return func(t *Transport) error {
if t == nil {
return fmt.Errorf("http long poll target option can not set nil transport")
}
targetUrl = strings.TrimSpace(targetUrl)
if targetUrl != "" {
var err error
var target *url.URL
target, err = url.Parse(targetUrl)
if err != nil {
return fmt.Errorf("http long poll target option failed to parse target url: %s", err.Error())
}
if t.LongPollReq == nil {
t.LongPollReq = &nethttp.Request{
Method: nethttp.MethodGet,
}
}
t.LongPollReq.URL = target
return nil
}
return fmt.Errorf("http long poll target option was empty string")
}
}