-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
146 lines (125 loc) · 3.87 KB
/
handler.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"firebase.google.com/go/messaging"
"github.com/julienschmidt/httprouter"
"github.com/prometheus/alertmanager/template"
)
// JSONResponse is the standard response struct
type JSONResponse struct {
Status int
Message string
}
type genericJSONRequest struct {
Title string
Body string
Notification bool
}
func indexHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Write([]byte(`<html>
<head><title>alertmanager-webhook-fcm</title></head>
<body>
<h1>alertmanager-webhook-fcm</h1>
<p><a href="/metrics">Metrics</a></p>
<p><a href="/alert/<topic>">Alertmanager webhook</a></p>
<p><a href="/generic/<topic>">Generic webhook</a></p>
</body>
</html>`))
}
func genericHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
var message *messaging.Message
data, err := readGenericRequestBody(req)
if err != nil {
log.Printf("Error parsing request body: %v", err)
sendJSONResponse(w, req, ps, http.StatusBadRequest, err.Error())
return
}
topic := getParamTopic(ps)
if data.Notification {
message = NewNotificationMessage(topic, data.Title, data.Body)
} else {
message = NewDataMessage(topic, data.Title, data.Body)
}
msg, err := fcmClient.Send(req.Context(), message)
fcmMessages.WithLabelValues(ps.MatchedRoutePath(), topic).Inc()
if err != nil {
fcmErrors.WithLabelValues(ps.MatchedRoutePath(), topic).Inc()
sendJSONResponse(w, req, ps, http.StatusInternalServerError, err.Error())
return
}
sendJSONResponse(w, req, ps,
http.StatusOK,
fmt.Sprintf("Message delivered to topic %s: %s", topic, msg),
)
}
func alertHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
templateData, err := readAlertRequestBody(req)
if err != nil {
log.Printf("Error parsing request body: %v", err)
sendJSONResponse(w, req, ps, http.StatusBadRequest, err.Error())
return
}
topic := getParamTopic(ps)
msg, err := processFcmMessage(req.Context(), ps.MatchedRoutePath(), topic, templateData)
if err != nil {
log.Printf("Error sending fcm message: %v", err)
sendJSONResponse(w, req, ps, http.StatusInternalServerError, err.Error())
return
}
sendJSONResponse(w, req, ps,
http.StatusOK,
fmt.Sprintf("Message delivered to topic %s: %s", topic, msg),
)
}
// getParamTopic returns the topic from httprouter.Params or "default"
func getParamTopic(ps httprouter.Params) string {
topic := ps.ByName("topic")
if topic == "" {
topic = "default"
}
return topic
}
func readGenericRequestBody(req *http.Request) (*genericJSONRequest, error) {
defer req.Body.Close()
data := genericJSONRequest{}
err := json.NewDecoder(req.Body).Decode(&data)
return &data, err
}
func readAlertRequestBody(req *http.Request) (*template.Data, error) {
defer req.Body.Close()
data := template.Data{}
err := json.NewDecoder(req.Body).Decode(&data)
return &data, err
}
func processFcmMessage(ctx context.Context, routePath, topic string, m *template.Data) (string, error) {
message, err := NewMessageFromAlertmanagerData(topic, m)
if err != nil {
te, _ := err.(*TemplateError)
templateErrors.WithLabelValues(te.Type).Inc()
return "", err
}
// Send a message to the devices subscribed to the topic.
fcmResponse, err := fcmClient.Send(ctx, message)
fcmMessages.WithLabelValues(routePath, topic).Inc()
if err != nil {
fcmErrors.WithLabelValues(routePath, topic).Inc()
}
return fcmResponse, err
}
func sendJSONResponse(w http.ResponseWriter, req *http.Request, ps httprouter.Params, statusCode int, message string) {
webhookRequests.WithLabelValues(req.Method, ps.MatchedRoutePath(), strconv.Itoa(statusCode)).Inc()
response, _ := json.Marshal(JSONResponse{
Status: statusCode,
Message: message,
})
w.WriteHeader(statusCode)
_, err := w.Write(response)
if err != nil {
log.Printf("Error sending response to client: %v", err)
}
}