/
options.go
164 lines (149 loc) · 4.3 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
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(*Protocol) error
// WithTarget sets the outbound recipient of cloudevents when using an HTTP
// request.
func WithTarget(targetUrl string) Option {
return func(p *Protocol) error {
if p == nil {
return fmt.Errorf("http protocol option can not set nil protocol")
}
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())
}
p.Target = target
if p.RequestTemplate == nil {
p.RequestTemplate = &nethttp.Request{
Method: nethttp.MethodPost,
}
}
p.RequestTemplate.URL = target
return nil
}
return fmt.Errorf("http target 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(p *Protocol) error {
if p == nil {
return fmt.Errorf("http header option can not set nil transport")
}
key = strings.TrimSpace(key)
if key != "" {
if p.RequestTemplate == nil {
p.RequestTemplate = &nethttp.Request{
Method: nethttp.MethodPost,
}
}
if p.RequestTemplate.Header == nil {
p.RequestTemplate.Header = nethttp.Header{}
}
p.RequestTemplate.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 *Protocol) error {
if t == nil {
return fmt.Errorf("http shutdown timeout option can not set nil transport")
}
t.ShutdownTimeout = &timeout
return nil
}
}
func checkListen(t *Protocol, 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 *Protocol) error {
if t == nil {
return fmt.Errorf("http port option can not set nil transport")
}
if port < 0 || port > 65535 {
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 *Protocol) 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 *Protocol) 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 *Protocol) error {
if t == nil {
return fmt.Errorf("http middleware option can not set nil transport")
}
t.middleware = append(t.middleware, middleware)
return nil
}
}
// WithHTTPTransport sets the HTTP client transport.
func WithHTTPTransport(httpTransport nethttp.RoundTripper) Option {
return func(t *Protocol) error {
t.transport = httpTransport
return nil
}
}