slackParserFunc, parserFunc with Response

Slack messages are acknowledged with code 200, message 'ok'
code statically answered with 204 and empty message before
slackParserFunc - rudimentary slack webhook parser
master
Sven Speckmaier 2020-01-02 22:49:12 +01:00
rodzic 1c012e1f86
commit 8afb5b9e6e
1 zmienionych plików z 24 dodań i 13 usunięć

Wyświetl plik

@ -6,8 +6,17 @@ import (
"net/http"
)
type Response struct {
Message string
Code int
}
func errorResponse() (Response) {
return Response{"", http.StatusInternalServerError}
}
// interface for parser functions (grafana, prometheus, ...)
type parserFunc func(*http.Request) (string, error)
type parserFunc func(*http.Request) (string, error, Response)
type messageHandler struct {
messages chan<- string // chan to xmpp client
@ -17,13 +26,14 @@ type messageHandler struct {
// http request handler
func (h *messageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// parse/generate message from http request
m, err := h.parserFunc(r)
m, err, response := h.parserFunc(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
// send message to xmpp client
h.messages <- m
w.WriteHeader(http.StatusNoContent)
w.WriteHeader(response.Code)
w.Write( []byte(response.Message) )
}
// returns new handler with a given parser function
@ -37,11 +47,11 @@ func newMessageHandler(m chan<- string, f parserFunc) *messageHandler {
/*************
GRAFANA PARSER
*************/
func grafanaParserFunc(r *http.Request) (string, error) {
func grafanaParserFunc(r *http.Request) (string, error, Response) {
// get alert data from request
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return "", err
return "", err, errorResponse()
}
// grafana alert struct
@ -55,7 +65,7 @@ func grafanaParserFunc(r *http.Request) (string, error) {
// parse body into the alert struct
err = json.Unmarshal(body, &alert)
if err != nil {
return "", err
return "", err, errorResponse()
}
// contruct alert message
@ -67,7 +77,7 @@ func grafanaParserFunc(r *http.Request) (string, error) {
message = ":( " + alert.Title + "\n" + alert.Message + "\n" + alert.RuleURL
}
return message, nil
return message, nil, Response{"", http.StatusNoContent}
}
/*************
@ -96,11 +106,11 @@ func nonemptyAppendNewline(message string) (string) {
return message+"\n"
}
func slackParserFunc(r *http.Request) (string, error) {
func slackParserFunc(r *http.Request) (string, error, Response) {
// get alert data from request
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return "", err
return "", err, errorResponse()
}
// grafana alert struct
@ -109,7 +119,7 @@ func slackParserFunc(r *http.Request) (string, error) {
// parse body into the alert struct
err = json.Unmarshal(body, &alert)
if err != nil {
return "", err
return "", err, Response{"", http.StatusInternalServerError}
}
// contruct alert message
@ -121,10 +131,11 @@ func slackParserFunc(r *http.Request) (string, error) {
for _, attachment := range alert.Attachments {
message = nonemptyAppendNewline(message)
message = message + attachment.Title+": "+attachment.TitleLink+"\n"
message = message + attachment.Text
message += attachment.Title+"\n"
message += attachment.TitleLink+"\n\n"
message += attachment.Text
}
return message, nil
return message, nil, Response{"ok", http.StatusOK}
}