-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.go
42 lines (36 loc) · 833 Bytes
/
watcher.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
package oplord
import (
"bytes"
"encoding/json"
"github.com/Clever/mgotail"
"net/http"
)
type OplogMatcher func(mgotail.Oplog) bool
type OplogAction func(mgotail.Oplog) error
type Watcher struct {
Matcher OplogMatcher
Action OplogAction
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func SimpleMatcherFactory(collection string, operations []string) OplogMatcher {
return func(log mgotail.Oplog) bool {
return collection == log.Namespace && stringInSlice(log.Operation, operations)
}
}
func SimplePostActionFactory(url string) OplogAction {
return func(log mgotail.Oplog) error {
b, err := json.Marshal(log.Object)
if err != nil {
return err
}
go http.Post(url, "application/json", bytes.NewBuffer(b))
return nil
}
}