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