xmpp-webhook/handler.go

64 wiersze
1.4 KiB
Go
Czysty Zwykły widok Historia

2017-09-26 12:39:19 +00:00
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
)
// interface for parser functions (grafana, prometheus, ...)
type parserFunc func(*http.Request) (string, error)
type messageHandler struct {
messages chan<- string // chan to xmpp client
parserFunc parserFunc
}
// http request handler
func (h *messageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// parse/generate message from http request
m, err := h.parserFunc(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
// send message to xmpp client
h.messages <- m
w.WriteHeader(http.StatusNoContent)
}
// returns new handler with a given parser function
func newMessageHandler(m chan<- string, f parserFunc) *messageHandler {
return &messageHandler{
messages: m,
parserFunc: f,
}
}
/*************
GRAFANA PARSER
*************/
func grafanaParserFunc(r *http.Request) (string, error) {
// get alert data from request
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return "", err
}
// grafana alert struct
alert := &struct {
Title string `json:"title"`
RuleURL string `json:"ruleUrl"`
State string `json:"state"`
Message string `json:"message"`
}{}
// parse body into the alert struct
err = json.Unmarshal(body, &alert)
if err != nil {
return "", err
}
// contruct and return alert message
return alert.State + ": " + alert.Title + "/" + alert.Message + "(" + alert.RuleURL + ")", nil
}