mirror of https://github.com/statping/statping
notifier api
parent
49ecf9a386
commit
52756ee359
2
Makefile
2
Makefile
|
@ -1,4 +1,4 @@
|
||||||
VERSION=0.79.2
|
VERSION=0.79.3
|
||||||
BINARY_NAME=statup
|
BINARY_NAME=statup
|
||||||
GOPATH:=$(GOPATH)
|
GOPATH:=$(GOPATH)
|
||||||
GOCMD=go
|
GOCMD=go
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
package notifier
|
package notifier
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -46,7 +47,7 @@ type Notification struct {
|
||||||
Var2 string `gorm:"not null;column:var2" json:"var2,omitempty"`
|
Var2 string `gorm:"not null;column:var2" json:"var2,omitempty"`
|
||||||
ApiKey string `gorm:"not null;column:api_key" json:"api_key,omitempty"`
|
ApiKey string `gorm:"not null;column:api_key" json:"api_key,omitempty"`
|
||||||
ApiSecret string `gorm:"not null;column:api_secret" json:"api_secret,omitempty"`
|
ApiSecret string `gorm:"not null;column:api_secret" json:"api_secret,omitempty"`
|
||||||
Enabled bool `gorm:"column:enabled;type:boolean;default:false" json:"enabled"`
|
Enabled sql.NullBool `gorm:"column:enabled;type:boolean;default:false" json:"enabled"`
|
||||||
Limits int `gorm:"not null;column:limits" json:"limits"`
|
Limits int `gorm:"not null;column:limits" json:"limits"`
|
||||||
Removable bool `gorm:"column:removable" json:"-"`
|
Removable bool `gorm:"column:removable" json:"-"`
|
||||||
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
|
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
|
||||||
|
@ -199,11 +200,14 @@ func SelectNotification(n Notifier) (*Notification, error) {
|
||||||
|
|
||||||
// Update will update the notification into the database
|
// Update will update the notification into the database
|
||||||
func Update(n Notifier, notif *Notification) (*Notification, error) {
|
func Update(n Notifier, notif *Notification) (*Notification, error) {
|
||||||
|
notif.ResetQueue()
|
||||||
err := db.Model(&Notification{}).Update(notif)
|
err := db.Model(&Notification{}).Update(notif)
|
||||||
if notif.Enabled {
|
if notif.Enabled.Bool {
|
||||||
notif.close()
|
notif.close()
|
||||||
notif.start()
|
notif.start()
|
||||||
go Queue(n)
|
go Queue(n)
|
||||||
|
} else {
|
||||||
|
notif.close()
|
||||||
}
|
}
|
||||||
return notif, err.Error
|
return notif, err.Error
|
||||||
}
|
}
|
||||||
|
@ -253,7 +257,7 @@ func startAllNotifiers() {
|
||||||
for _, comm := range AllCommunications {
|
for _, comm := range AllCommunications {
|
||||||
if isType(comm, new(Notifier)) {
|
if isType(comm, new(Notifier)) {
|
||||||
notify := comm.(Notifier)
|
notify := comm.(Notifier)
|
||||||
if notify.Select().Enabled {
|
if notify.Select().Enabled.Bool {
|
||||||
notify.Select().close()
|
notify.Select().close()
|
||||||
notify.Select().start()
|
notify.Select().start()
|
||||||
go Queue(notify)
|
go Queue(notify)
|
||||||
|
@ -376,8 +380,8 @@ func isType(n interface{}, obj interface{}) bool {
|
||||||
|
|
||||||
// isEnabled returns true if the notifier is enabled
|
// isEnabled returns true if the notifier is enabled
|
||||||
func isEnabled(n interface{}) bool {
|
func isEnabled(n interface{}) bool {
|
||||||
notifier, _ := SelectNotification(n.(Notifier))
|
notifier := n.(Notifier).Select()
|
||||||
return notifier.Enabled
|
return notifier.Enabled.Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// inLimits will return true if the notifier is within sending limits
|
// inLimits will return true if the notifier is within sending limits
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
@ -341,7 +342,7 @@ func apiNotifierGetHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(notifierObj.Select())
|
json.NewEncoder(w).Encode(notifierObj)
|
||||||
}
|
}
|
||||||
|
|
||||||
func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -351,33 +352,34 @@ func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
var notification *notifier.Notification
|
var notification *notifier.Notification
|
||||||
|
fmt.Println(r.Body)
|
||||||
decoder := json.NewDecoder(r.Body)
|
decoder := json.NewDecoder(r.Body)
|
||||||
decoder.Decode(¬ification)
|
decoder.Decode(¬ification)
|
||||||
|
|
||||||
notif, not, err := notifier.SelectNotifier(vars["notifier"])
|
notifer, not, err := notifier.SelectNotifier(vars["notifier"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, fmt.Sprintf("%v notifier was not found", vars["notifier"]), http.StatusInternalServerError)
|
http.Error(w, fmt.Sprintf("%v notifier was not found", vars["notifier"]), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
notif.Var1 = notification.Var1
|
notifer.Var1 = notification.Var1
|
||||||
notif.Var2 = notification.Var2
|
notifer.Var2 = notification.Var2
|
||||||
notif.Host = notification.Host
|
notifer.Host = notification.Host
|
||||||
notif.Port = notification.Port
|
notifer.Port = notification.Port
|
||||||
notif.Password = notification.Password
|
notifer.Password = notification.Password
|
||||||
notif.Username = notification.Username
|
notifer.Username = notification.Username
|
||||||
notif.Enabled = notification.Enabled
|
notifer.Enabled = sql.NullBool{notification.Enabled.Bool, true}
|
||||||
notif.ApiKey = notification.ApiKey
|
notifer.ApiKey = notification.ApiKey
|
||||||
notif.ApiSecret = notification.ApiSecret
|
notifer.ApiSecret = notification.ApiSecret
|
||||||
|
|
||||||
_, err = notifier.Update(not, notif)
|
_, err = notifier.Update(not, notifer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Log(3, fmt.Sprintf("issue updating notifier: %v", err))
|
utils.Log(3, fmt.Sprintf("issue updating notifier: %v", err))
|
||||||
}
|
}
|
||||||
notifier.OnSave(notif.Method)
|
notifier.OnSave(notifer.Method)
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(notif)
|
json.NewEncoder(w).Encode(notifer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func isAPIAuthorized(r *http.Request) bool {
|
func isAPIAuthorized(r *http.Request) bool {
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/hunterlong/statup/core"
|
"github.com/hunterlong/statup/core"
|
||||||
|
@ -188,7 +189,8 @@ func saveNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if limits != 0 {
|
if limits != 0 {
|
||||||
notifer.Limits = limits
|
notifer.Limits = limits
|
||||||
}
|
}
|
||||||
notifer.Enabled = enabled == "on"
|
notifer.Enabled = sql.NullBool{enabled == "on", true}
|
||||||
|
|
||||||
_, err = notifier.Update(notif, notifer)
|
_, err = notifier.Update(notif, notifer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Log(3, fmt.Sprintf("issue updating notifier: %v", err))
|
utils.Log(3, fmt.Sprintf("issue updating notifier: %v", err))
|
||||||
|
@ -253,7 +255,7 @@ func testNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if limits != 0 {
|
if limits != 0 {
|
||||||
notifer.Limits = limits
|
notifer.Limits = limits
|
||||||
}
|
}
|
||||||
notifer.Enabled = enabled == "on"
|
notifer.Enabled = sql.NullBool{enabled == "on", true}
|
||||||
|
|
||||||
err = notif.(notifier.Tester).OnTest()
|
err = notif.(notifier.Tester).OnTest()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
|
@ -33,7 +33,7 @@
|
||||||
|
|
||||||
<div class="col-3 col-sm-2 mt-1">
|
<div class="col-3 col-sm-2 mt-1">
|
||||||
<span class="switch">
|
<span class="switch">
|
||||||
<input type="checkbox" name="enable" class="switch" id="switch-{{ $n.Method }}" {{if $n.Enabled}}checked{{end}}>
|
<input type="checkbox" name="enable" class="switch" id="switch-{{ $n.Method }}" {{if $n.Enabled.Bool}}checked{{end}}>
|
||||||
<label for="switch-{{ $n.Method }}"></label>
|
<label for="switch-{{ $n.Method }}"></label>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue