-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttp_handlers.go
123 lines (111 loc) · 3.13 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
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
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"log"
"net/http/httputil"
)
func healthz(c *gin.Context) {
c.JSON(200, gin.H{
"message": "ok",
})
}
func webhook(c *gin.Context) {
if viper.GetBool("debug") {
// Save a copy of this request for debugging.
requestDump, err := httputil.DumpRequest(c.Request, true)
if err != nil {
fmt.Println(err)
}
log.Printf("New request")
fmt.Println(string(requestDump))
}
var m HookMessage
if c.ShouldBindJSON(&m) == nil {
log.Printf("Alerts: GroupLabels=%v, CommonLabels=%v", m.GroupLabels, m.CommonLabels)
for _, alert := range m.Alerts {
if prevAlert, founded := FindAlert(alert); founded {
alert.Channel = prevAlert.Channel
alert.MessageTS = prevAlert.MessageTS
alert.MessageBody = prevAlert.MessageBody
respChannel, respTimestamp, _, err := alert.PostMessage()
if err != nil {
c.String(500, "%v", err)
return
}
if alert.Status == AlertStatusFiring {
alert.MessageTS = respTimestamp
alert.Channel = respChannel
AddAlert(alert)
}
log.Printf("Slack update sended, channel: %s thread: %s", respChannel, respTimestamp)
} else {
// post new message
respChannel, respTimestamp, messageBody, err := alert.PostMessage()
if err != nil {
c.String(500, "%v", err)
return
}
alert.MessageTS = respTimestamp
alert.Channel = respChannel
alert.MessageBody = messageBody
AddAlert(alert)
}
}
c.JSON(200, map[string]string{"success": "true"})
return
}
c.JSON(400, map[string]string{"status": "Invalid body of request"})
}
//
//func webhook(w http.ResponseWriter, r *http.Request) {
// if viper.GetBool("debug") {
// // Save a copy of this request for debugging.
// requestDump, err := httputil.DumpRequest(r, true)
// if err != nil {
// fmt.Println(err)
// }
// log.Printf("New request")
// fmt.Println(string(requestDump))
// }
//
// dec := json.NewDecoder(r.Body)
// defer r.Body.Close()
//
// var m HookMessage
// if err := dec.Decode(&m); err != nil {
// log.Printf("error decoding message: %v", err)
// http.Error(w, "invalid request body", 400)
// return
// }
//
// log.Printf("Alerts: GroupLabels=%v, CommonLabels=%v", m.GroupLabels, m.CommonLabels)
//
// for _, alert := range m.Alerts {
// if prevAlert, founded := FindAlert(alert); founded {
// alert.Channel = prevAlert.Channel
// alert.MessageTS = prevAlert.MessageTS
// alert.MessageBody = prevAlert.MessageBody
// respChannel, respTimestamp, _ := alert.PostMessage()
// if alert.Status == AlertStatusFiring {
// alert.MessageTS = respTimestamp
// alert.Channel = respChannel
// AddAlert(alert)
// }
// log.Printf("Slack update sended, channel: %s thread: %s", respChannel, respTimestamp)
// } else {
// // post new message
// respChannel, respTimestamp, messageBody := alert.PostMessage()
// alert.MessageTS = respTimestamp
// alert.Channel = respChannel
// alert.MessageBody = messageBody
//
// AddAlert(alert)
// }
// }
//
// w.Header().Set("Content-Type", "application/json")
// _, err := w.Write([]byte("{\"success\": true}"))
// fatal(err, "failed to send response")
//}