瀏覽代碼

Separate out http listener endpoints

tags/v0.1.0
Russ Garrett 7 年之前
父節點
當前提交
4665272fd6
沒有連結到貢獻者的電子郵件帳戶。
共有 2 個檔案被更改,包括 42 行新增36 行删除
  1. 42
    0
      httplistener/grafana.go
  2. 0
    36
      httplistener/httplistener.go

+ 42
- 0
httplistener/grafana.go 查看文件

@@ -0,0 +1,42 @@
1
+package httplistener
2
+
3
+import (
4
+	"bytes"
5
+	"encoding/json"
6
+	"fmt"
7
+	"github.com/spf13/viper"
8
+	"net/http"
9
+)
10
+
11
+type grafanaMatch struct {
12
+	Metric string
13
+	Value  float32
14
+}
15
+
16
+type grafanaAlert struct {
17
+	Title       string
18
+	RuleName    string
19
+	RuleUrl     string
20
+	State       string
21
+	ImageUrl    string
22
+	Message     string
23
+	EvalMatches []grafanaMatch
24
+}
25
+
26
+func (hl *HTTPListener) grafanaAlertHandler(w http.ResponseWriter, request *http.Request) {
27
+	if request.Method != "POST" {
28
+		http.NotFound(w, request)
29
+		return
30
+	}
31
+
32
+	var alert grafanaAlert
33
+	buf := new(bytes.Buffer)
34
+	buf.ReadFrom(request.Body)
35
+	json.Unmarshal(buf.Bytes(), &alert)
36
+	msg := fmt.Sprintf("[Grafana] [%s] %s: %s.", alert.State, alert.RuleName, alert.Message)
37
+	for _, match := range alert.EvalMatches {
38
+		msg += fmt.Sprintf(" %s:%f", match.Metric, match.Value)
39
+	}
40
+	msg += " " + alert.RuleUrl
41
+	hl.irc.Privmsgf(viper.GetString("http.listeners.grafana"), msg)
42
+}

+ 0
- 36
httplistener/httplistener.go 查看文件

@@ -1,9 +1,6 @@
1 1
 package httplistener
2 2
 
3 3
 import (
4
-	"bytes"
5
-	"encoding/json"
6
-	"fmt"
7 4
 	"github.com/juju/loggo"
8 5
 	"github.com/spf13/viper"
9 6
 	"github.com/thoj/go-ircevent"
@@ -32,36 +29,3 @@ func New(irc *irc.Connection) (*HTTPListener, error) {
32 29
 	go hl.http.ListenAndServe()
33 30
 	return &hl, nil
34 31
 }
35
-
36
-type grafanaMatch struct {
37
-	Metric string
38
-	Value  float32
39
-}
40
-
41
-type grafanaAlert struct {
42
-	Title       string
43
-	RuleName    string
44
-	RuleUrl     string
45
-	State       string
46
-	ImageUrl    string
47
-	Message     string
48
-	EvalMatches []grafanaMatch
49
-}
50
-
51
-func (hl *HTTPListener) grafanaAlertHandler(w http.ResponseWriter, request *http.Request) {
52
-	if request.Method != "POST" {
53
-		http.NotFound(w, request)
54
-		return
55
-	}
56
-
57
-	var alert grafanaAlert
58
-	buf := new(bytes.Buffer)
59
-	buf.ReadFrom(request.Body)
60
-	json.Unmarshal(buf.Bytes(), &alert)
61
-	msg := fmt.Sprintf("[Grafana] [%s] %s: %s.", alert.State, alert.RuleName, alert.Message)
62
-	for _, match := range alert.EvalMatches {
63
-		msg += fmt.Sprintf(" %s:%f", match.Metric, match.Value)
64
-	}
65
-	msg += " " + alert.RuleUrl
66
-	hl.irc.Privmsgf(viper.GetString("http.listeners.grafana"), msg)
67
-}

Loading…
取消
儲存