@@ -23,11 +23,16 @@ func (c *ClientMock) Do(req *http.Request) (*http.Response, error) {
23
23
return & http.Response {}, nil
24
24
}
25
25
26
- type WebhookI interface {
27
- CheckMsg (* wrp.Message ) error
26
+ // move to subpackage and change to Interface
27
+ type Matcher interface {
28
+ IsMatch (* wrp.Message ) bool
29
+
30
+ //TODO: not sure if this will be functionality of all webhooks or just v1
31
+ //leaving for now - will make changes if running into roadblock with this
28
32
getUrls () * ring.Ring
29
33
}
30
- type WebhookV1 struct {
34
+
35
+ type MatcherV1 struct {
31
36
events []* regexp.Regexp
32
37
matcher []* regexp.Regexp
33
38
urls * ring.Ring
@@ -41,10 +46,10 @@ type CommonWebhook struct {
41
46
42
47
// Update applies user configurable values for the outbound sender when a
43
48
// webhook is registered
44
- func (w1 * WebhookV1 ) Update (l ListenerV1 ) error {
49
+ func (m1 * MatcherV1 ) Update (l ListenerV1 ) error {
45
50
46
51
//TODO: don't believe the logger for webhook is being set anywhere just yet
47
- w1 .logger = w1 .logger .With (zap .String ("webhook.address" , l .Registration .Address ))
52
+ m1 .logger = m1 .logger .With (zap .String ("webhook.address" , l .Registration .Address ))
48
53
49
54
if l .Registration .FailureURL != "" {
50
55
_ , err := url .ParseRequestURI (l .Registration .FailureURL )
@@ -86,54 +91,54 @@ func (w1 *WebhookV1) Update(l ListenerV1) error {
86
91
for i := 0 ; i < urlCount ; i ++ {
87
92
_ , err := url .Parse (l .Registration .Config .AlternativeURLs [i ])
88
93
if err != nil {
89
- w1 .logger .Error ("failed to update url" , zap .Any ("url" , l .Registration .Config .AlternativeURLs [i ]), zap .Error (err ))
94
+ m1 .logger .Error ("failed to update url" , zap .Any ("url" , l .Registration .Config .AlternativeURLs [i ]), zap .Error (err ))
90
95
return err
91
96
}
92
97
}
93
98
94
99
// write/update sink sender
95
- w1 .mutex .Lock ()
96
- defer w1 .mutex .Unlock ()
100
+ m1 .mutex .Lock ()
101
+ defer m1 .mutex .Unlock ()
97
102
98
- w1 .events = events
103
+ m1 .events = events
99
104
100
105
//TODO: need to figure out how to set this
101
106
102
107
// if matcher list is empty set it nil for Queue() logic
103
- w1 .matcher = nil
108
+ m1 .matcher = nil
104
109
if 0 < len (matcher ) {
105
- w1 .matcher = matcher
110
+ m1 .matcher = matcher
106
111
}
107
112
108
113
if urlCount == 0 {
109
- w1 .urls = ring .New (1 )
110
- w1 .urls .Value = l .Registration .Config .ReceiverURL
114
+ m1 .urls = ring .New (1 )
115
+ m1 .urls .Value = l .Registration .Config .ReceiverURL
111
116
} else {
112
117
ring := ring .New (urlCount )
113
118
for i := 0 ; i < urlCount ; i ++ {
114
119
ring .Value = l .Registration .Config .AlternativeURLs [i ]
115
120
ring = ring .Next ()
116
121
}
117
- w1 .urls = ring
122
+ m1 .urls = ring
118
123
}
119
124
120
125
// Randomize where we start so all the instances don't synchronize
121
126
rand := rand .New (rand .NewSource (time .Now ().UnixNano ()))
122
- offset := rand .Intn (w1 .urls .Len ())
127
+ offset := rand .Intn (m1 .urls .Len ())
123
128
for 0 < offset {
124
- w1 .urls = w1 .urls .Next ()
129
+ m1 .urls = m1 .urls .Next ()
125
130
offset --
126
131
}
127
132
128
133
return nil
129
134
130
135
}
131
136
132
- func (w1 * WebhookV1 ) CheckMsg (msg * wrp.Message ) ( err error ) {
133
- w1 .mutex .RLock ()
134
- events := w1 .events
135
- matcher := w1 .matcher
136
- w1 .mutex .RUnlock ()
137
+ func (m1 * MatcherV1 ) IsMatch (msg * wrp.Message ) bool {
138
+ m1 .mutex .RLock ()
139
+ events := m1 .events
140
+ matcher := m1 .matcher
141
+ m1 .mutex .RUnlock ()
137
142
138
143
var (
139
144
matchEvent bool
@@ -146,9 +151,8 @@ func (w1 *WebhookV1) CheckMsg(msg *wrp.Message) (err error) {
146
151
}
147
152
}
148
153
if ! matchEvent {
149
- w1 .logger .Debug ("destination regex doesn't match" , zap .String ("event.dest" , msg .Destination ))
150
- //TODO: return an error here?
151
- return
154
+ m1 .logger .Debug ("destination regex doesn't match" , zap .String ("event.dest" , msg .Destination ))
155
+ return false
152
156
}
153
157
154
158
if matcher != nil {
@@ -162,37 +166,17 @@ func (w1 *WebhookV1) CheckMsg(msg *wrp.Message) (err error) {
162
166
}
163
167
164
168
if ! matchDevice {
165
- w1 .logger .Debug ("device regex doesn't match" , zap .String ("event.source" , msg .Source ))
166
- //TODO: return an error here?
167
- return
169
+ m1 .logger .Debug ("device regex doesn't match" , zap .String ("event.source" , msg .Source ))
170
+ return false
168
171
}
169
- return
172
+ return true
170
173
}
171
174
172
- func (w1 * WebhookV1 ) getUrls () (urls * ring.Ring ) {
173
- urls = w1 .urls
175
+ func (m1 * MatcherV1 ) getUrls () (urls * ring.Ring ) {
176
+ urls = m1 .urls
174
177
// Move to the next URL to try 1st the next time.
175
178
// This is okay because we run a single dispatcher and it's the
176
179
// only one updating this field.
177
- w1 .urls = w1 .urls .Next ()
180
+ m1 .urls = m1 .urls .Next ()
178
181
return
179
182
}
180
-
181
- type WebhookV2 struct {
182
- //nolint:staticcheck
183
- placeholder string
184
- CommonWebhook
185
- }
186
-
187
- func (w2 * WebhookV2 ) Update (l ListenerV2 ) error {
188
-
189
- return nil
190
- }
191
-
192
- func (w2 * WebhookV2 ) CheckMsg (msg * wrp.Message ) error {
193
- return nil
194
- }
195
-
196
- func (w2 * WebhookV2 ) getUrls () * ring.Ring {
197
- return nil
198
- }
0 commit comments