-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathevent.go
86 lines (70 loc) · 1.87 KB
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/WeTrustPlatform/blockform/model"
"goji.io/pat"
)
// To try this:
// curl -X POST -F 'title=Geth has been restarted' -F 'type=issue' -F 'description=The blocknumber was lagging behind etherscan for more than 200 blocks' http://localhost:3000/node/:id/event/:apikey
func handleEvent(w http.ResponseWriter, r *http.Request) {
ID := pat.Param(r, "id")
APIKey := pat.Param(r, "apikey")
node := model.Node{}
db.Find(&node, ID)
if node.APIKey != APIKey {
w.WriteHeader(403)
return
}
title := r.FormValue("title")
type0 := r.FormValue("type")
description := r.FormValue("description")
if title == "" || (type0 != model.Issue && type0 != model.Fine) || description == "" {
w.WriteHeader(400)
return
}
event := model.Event{
NodeID: node.ID,
Type: type0,
Title: title,
Description: description,
}
db.Create(&event)
go notifySlack(node, event)
}
func notifySlack(node model.Node, event model.Event) {
url := os.Getenv("SLACK_HOOK")
// only notify issues, and only of the SLACK_HOOK is set
if event.Type != model.Issue || url == "" {
return
}
nodeURL := fmt.Sprintf("%s/node/%d/activity", os.Getenv("SITE_URL"), node.ID)
json := []byte(`{
"attachments": [
{
"pretext": "On node ` + node.Name + `",
"title": "` + event.Title + `",
"title_link": "` + nodeURL + `",
"text": "` + event.Description + `",
"color": "#dc3545"
}
]
}`)
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(json))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
log.Println("response Status:", resp.Status)
log.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
log.Println("response Body:", string(body))
}