xmpp-webhook

webhook to xmpp gateway (clone of https://github.com/opthomas-prime/xmpp-webhook/ with my own mods)
git clone https://git.e1e0.net/xmpp-webhook.git
Log | Files | Refs | README | LICENSE

handler.go (2753B)


      1 package main
      2 
      3 import (
      4 	"encoding/json"
      5 	"io/ioutil"
      6 	"net/http"
      7 )
      8 
      9 // interface for parser functions (grafana, prometheus, ...)
     10 type parserFunc func(*http.Request) (string, error)
     11 
     12 type messageHandler struct {
     13 	messages   chan<- string // chan to xmpp client
     14 	parserFunc parserFunc
     15 }
     16 
     17 // http request handler
     18 func (h *messageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
     19 	// parse/generate message from http request
     20 	m, err := h.parserFunc(r)
     21 	if err != nil {
     22 		w.WriteHeader(http.StatusInternalServerError)
     23 	}
     24 	// send message to xmpp client
     25 	h.messages <- m
     26 	w.WriteHeader(http.StatusNoContent)
     27 }
     28 
     29 // returns new handler with a given parser function
     30 func newMessageHandler(m chan<- string, f parserFunc) *messageHandler {
     31 	return &messageHandler{
     32 		messages:   m,
     33 		parserFunc: f,
     34 	}
     35 }
     36 
     37 /*************
     38 GRAFANA PARSER
     39 *************/
     40 func grafanaParserFunc(r *http.Request) (string, error) {
     41 	// get alert data from request
     42 	body, err := ioutil.ReadAll(r.Body)
     43 	if err != nil {
     44 		return "", err
     45 	}
     46 
     47 	// grafana alert struct
     48 	alert := &struct {
     49 		Title   string `json:"title"`
     50 		RuleURL string `json:"ruleUrl"`
     51 		State   string `json:"state"`
     52 		Message string `json:"message"`
     53 	}{}
     54 
     55 	// parse body into the alert struct
     56 	err = json.Unmarshal(body, &alert)
     57 	if err != nil {
     58 		return "", err
     59 	}
     60 
     61 	// contruct alert message
     62 	var message string
     63 	switch alert.State {
     64 	case "ok":
     65 		message = ":) " + alert.Title
     66 	default:
     67 		message = ":( " + alert.Title + "\n" + alert.Message
     68 	}
     69 
     70 	return message, nil
     71 }
     72 
     73 /*************
     74 CUSTOM PARSER
     75 *************/
     76 func customParserFunc(r *http.Request) (string, error) {
     77 	// get alert data from request
     78 	body, err := ioutil.ReadAll(r.Body)
     79 	if err != nil {
     80 		return "", err
     81 	}
     82 
     83 	// grafana alert struct
     84 	alert := &struct {
     85 		Title   string `json:"title"`
     86 		Message string `json:"message"`
     87 	}{}
     88 
     89 	// parse body into the alert struct
     90 	err = json.Unmarshal(body, &alert)
     91 	if err != nil {
     92 		return "", err
     93 	}
     94 
     95 	// contruct alert message
     96 	var message string
     97 	message = "-> " + alert.Title + " <- " + "\n" + alert.Message
     98 
     99 	return message, nil
    100 }
    101 
    102 /****************
    103 PROMETHEUS PARSER
    104 ****************/
    105 func prometheusParserFunc(r *http.Request) (string, error) {
    106 	// get alert data from request
    107 	body, err := ioutil.ReadAll(r.Body)
    108 	if err != nil {
    109 		return "", err
    110 	}
    111 
    112 	// prometheus alert struct
    113 	alert := &struct {
    114 		Status      string `json:"status"`
    115 		ExternalURL string `json:"externalURL"`
    116 	}{}
    117 
    118 	// parse body into the alert struct
    119 	err = json.Unmarshal(body, &alert)
    120 	if err != nil {
    121 		return "", err
    122 	}
    123 
    124 	// contruct alert message
    125 	var message string
    126 	switch alert.Status {
    127 	case "resolved":
    128 		message = ":) " + alert.ExternalURL
    129 	default:
    130 		message = ":( " + alert.ExternalURL
    131 	}
    132 
    133 	return message, nil
    134 }