-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_handlers.go
73 lines (58 loc) · 1.67 KB
/
http_handlers.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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
)
// --------------------------------------
// a copy-paste from the ironhook package
// --------------------------------------
func WebhooksHandler(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, "/verification") {
VerificationHandler(w, r)
} else if strings.Contains(r.URL.Path, "/notification") {
NotificationHandler(w, r)
} else {
log.Print("Received an unrecognised request: ", r.URL.Path)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "")
return
}
}
func VerificationHandler(w http.ResponseWriter, r *http.Request) {
qID, ok := r.URL.Query()["id"]
if !ok || len(qID[0]) < 1 {
fmt.Fprintf(w, "URL Parameter <id> is missing")
return
}
log.Println("Received a verification request UUID: ", qID[0])
fmt.Fprint(w, qID[0])
}
func NotificationHandler(w http.ResponseWriter, r *http.Request) {
// Received a notification
// -----------------------
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Couldnt make head or tail of this")
return
}
var notif WebhookNotification
// or
// var notif ironhook.WebhookNotification
err = json.Unmarshal(body, ¬if)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "Couldnt make head or tail of this")
return
}
// Successfully parsed a webhook notification
// ------------------------------------------
log.Println("Received a notification UUID: ", notif.EventUUID)
log.Println(notif.EventUUID, " with topic: ", notif.Topic)
log.Println(notif.EventUUID, " with body: ", notif.Body)
fmt.Fprint(w, "Awesome, thanks")
}