notifier api

pull/94/head
Hunter Long 2018-11-06 04:12:17 -08:00
parent 49ecf9a386
commit 52756ee359
5 changed files with 31 additions and 23 deletions

View File

@ -1,4 +1,4 @@
VERSION=0.79.2
VERSION=0.79.3
BINARY_NAME=statup
GOPATH:=$(GOPATH)
GOCMD=go

View File

@ -16,6 +16,7 @@
package notifier
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
@ -46,7 +47,7 @@ type Notification struct {
Var2 string `gorm:"not null;column:var2" json:"var2,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"`
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"`
Removable bool `gorm:"column:removable" json:"-"`
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
func Update(n Notifier, notif *Notification) (*Notification, error) {
notif.ResetQueue()
err := db.Model(&Notification{}).Update(notif)
if notif.Enabled {
if notif.Enabled.Bool {
notif.close()
notif.start()
go Queue(n)
} else {
notif.close()
}
return notif, err.Error
}
@ -253,7 +257,7 @@ func startAllNotifiers() {
for _, comm := range AllCommunications {
if isType(comm, new(Notifier)) {
notify := comm.(Notifier)
if notify.Select().Enabled {
if notify.Select().Enabled.Bool {
notify.Select().close()
notify.Select().start()
go Queue(notify)
@ -376,8 +380,8 @@ func isType(n interface{}, obj interface{}) bool {
// isEnabled returns true if the notifier is enabled
func isEnabled(n interface{}) bool {
notifier, _ := SelectNotification(n.(Notifier))
return notifier.Enabled
notifier := n.(Notifier).Select()
return notifier.Enabled.Bool
}
// inLimits will return true if the notifier is within sending limits

View File

@ -16,6 +16,7 @@
package handlers
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
@ -341,7 +342,7 @@ func apiNotifierGetHandler(w http.ResponseWriter, r *http.Request) {
return
}
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) {
@ -351,33 +352,34 @@ func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) {
}
vars := mux.Vars(r)
var notification *notifier.Notification
fmt.Println(r.Body)
decoder := json.NewDecoder(r.Body)
decoder.Decode(&notification)
notif, not, err := notifier.SelectNotifier(vars["notifier"])
notifer, not, err := notifier.SelectNotifier(vars["notifier"])
if err != nil {
http.Error(w, fmt.Sprintf("%v notifier was not found", vars["notifier"]), http.StatusInternalServerError)
return
}
notif.Var1 = notification.Var1
notif.Var2 = notification.Var2
notif.Host = notification.Host
notif.Port = notification.Port
notif.Password = notification.Password
notif.Username = notification.Username
notif.Enabled = notification.Enabled
notif.ApiKey = notification.ApiKey
notif.ApiSecret = notification.ApiSecret
notifer.Var1 = notification.Var1
notifer.Var2 = notification.Var2
notifer.Host = notification.Host
notifer.Port = notification.Port
notifer.Password = notification.Password
notifer.Username = notification.Username
notifer.Enabled = sql.NullBool{notification.Enabled.Bool, true}
notifer.ApiKey = notification.ApiKey
notifer.ApiSecret = notification.ApiSecret
_, err = notifier.Update(not, notif)
_, err = notifier.Update(not, notifer)
if err != nil {
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")
json.NewEncoder(w).Encode(notif)
json.NewEncoder(w).Encode(notifer)
}
func isAPIAuthorized(r *http.Request) bool {

View File

@ -16,6 +16,7 @@
package handlers
import (
"database/sql"
"fmt"
"github.com/gorilla/mux"
"github.com/hunterlong/statup/core"
@ -188,7 +189,8 @@ func saveNotificationHandler(w http.ResponseWriter, r *http.Request) {
if limits != 0 {
notifer.Limits = limits
}
notifer.Enabled = enabled == "on"
notifer.Enabled = sql.NullBool{enabled == "on", true}
_, err = notifier.Update(notif, notifer)
if err != nil {
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 {
notifer.Limits = limits
}
notifer.Enabled = enabled == "on"
notifer.Enabled = sql.NullBool{enabled == "on", true}
err = notif.(notifier.Tester).OnTest()
if err == nil {

View File

@ -33,7 +33,7 @@
<div class="col-3 col-sm-2 mt-1">
<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>
</span>
</div>