forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifications.go
114 lines (97 loc) · 2.61 KB
/
notifications.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package handlers
import (
"github.com/gorilla/mux"
"github.com/statping/statping/types/errors"
"github.com/statping/statping/types/failures"
"github.com/statping/statping/types/notifications"
"github.com/statping/statping/types/services"
"net/http"
"sort"
)
func apiNotifiersHandler(w http.ResponseWriter, r *http.Request) {
var notifs []notifications.Notification
for _, n := range services.AllNotifiers() {
notif := n.Select()
no, err := notifications.Find(notif.Method)
if err != nil {
log.Error(err)
sendErrorJson(err, w, r)
}
notif.UpdateFields(no)
notifs = append(notifs, *notif)
}
sort.Sort(notifications.NotificationOrder(notifs))
returnJson(notifs, w, r)
}
func apiNotifierGetHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
notifer := services.FindNotifier(vars["notifier"])
if notifer == nil {
sendErrorJson(errors.New("could not find notifier"), w, r)
return
}
returnJson(notifer, w, r)
}
func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
notifer, err := notifications.Find(vars["notifier"])
if err != nil {
sendErrorJson(err, w, r)
return
}
if err := DecodeJSON(r, ¬ifer); err != nil {
sendErrorJson(err, w, r)
return
}
log.Infof("Updating %s Notifier", notifer.Title)
if err := notifer.Update(); err != nil {
sendErrorJson(err, w, r)
return
}
notif := services.ReturnNotifier(notifer.Method)
if err := notif.Valid(notifer.Values()); err != nil {
sendErrorJson(err, w, r)
return
}
if _, err := notif.OnSave(); err != nil {
sendErrorJson(err, w, r)
return
}
sendJsonAction(vars["notifier"], "update", w, r)
}
type testNotificationReq struct {
Method string `json:"method"`
Notification notifications.Notification `json:"notifier"`
}
func testNotificationHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
n := services.FindNotifier(vars["notifier"])
if n == nil {
sendErrorJson(errors.New("unknown notifier"), w, r)
return
}
var req testNotificationReq
if err := DecodeJSON(r, &req); err != nil {
sendErrorJson(err, w, r)
return
}
notif := services.ReturnNotifier(n.Method)
var out string
var err error
if req.Method == "success" {
out, err = notif.OnSuccess(services.Example(true))
} else {
out, err = notif.OnFailure(services.Example(false), failures.Example())
}
resp := ¬ifierTestResp{
Success: err == nil,
Response: out,
Error: err,
}
returnJson(resp, w, r)
}
type notifierTestResp struct {
Success bool `json:"success"`
Response string `json:"response,omitempty"`
Error error `json:"error,omitempty"`
}