statping/handlers/notifications.go

86 lines
1.9 KiB
Go
Raw Normal View History

package handlers
import (
"github.com/gorilla/mux"
2020-03-14 03:13:20 +00:00
"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
2020-03-14 03:13:20 +00:00
notifiers := services.AllNotifiers()
for _, n := range notifiers {
notif := n.Select()
notifer, _ := notifications.Find(notif.Method)
notif.UpdateFields(notifer)
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)
2020-03-25 18:46:50 +00:00
notif := services.FindNotifier(vars["notifier"])
notifer, err := notifications.Find(notif.Method)
if err != nil {
sendErrorJson(err, w, r)
return
}
returnJson(notifer, w, r)
}
func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
2020-03-14 03:13:20 +00:00
notifer, err := notifications.Find(vars["notifier"])
if err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-04 10:29:00 +00:00
2020-04-16 17:55:47 +00:00
if err := DecodeJSON(r, &notifer); err != nil {
sendErrorJson(err, w, r)
return
}
2020-04-16 17:55:47 +00:00
2020-03-12 04:58:56 +00:00
err = notifer.Update()
if err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-12 04:58:56 +00:00
//notifications.OnSave(notifer.Method)
sendJsonAction(vars["notifier"], "update", w, r)
}
func testNotificationHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
notifer, err := notifications.Find(vars["notifier"])
if err != nil {
sendErrorJson(err, w, r)
return
}
2020-04-16 17:55:47 +00:00
if err := DecodeJSON(r, &notifer); err != nil {
2020-03-12 04:58:56 +00:00
sendErrorJson(err, w, r)
return
}
notif := services.ReturnNotifier(notifer.Method)
out, err := notif.OnTest()
resp := &notifierTestResp{
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"`
}