-
Notifications
You must be signed in to change notification settings - Fork 13
/
mandrill_webhooks.go
56 lines (47 loc) · 1.21 KB
/
mandrill_webhooks.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
package mandrill
import (
"encoding/json"
"io"
"log"
"net/http"
"net/url"
"time"
"github.com/circonus-labs/circonus-unified-agent/cua"
"github.com/gorilla/mux"
)
type Webhook struct {
Path string
acc cua.Accumulator
}
func (md *Webhook) Register(router *mux.Router, acc cua.Accumulator) {
router.HandleFunc(md.Path, md.returnOK).Methods("HEAD")
router.HandleFunc(md.Path, md.eventHandler).Methods("POST")
log.Printf("I! Started the webhooks_mandrill on %s\n", md.Path)
md.acc = acc
}
func (md *Webhook) returnOK(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
func (md *Webhook) eventHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
data, err := url.ParseQuery(string(body))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
var events []Event
err = json.Unmarshal([]byte(data.Get("mandrill_events")), &events)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
for _, event := range events {
md.acc.AddFields("mandrill_webhooks", event.Fields(), event.Tags(), time.Unix(event.TimeStamp, 0))
}
w.WriteHeader(http.StatusOK)
}