notifier api

pull/94/head
Hunter Long 2018-11-06 01:03:21 -08:00
parent 3fc1ea39cf
commit 49ecf9a386
7 changed files with 145 additions and 20 deletions

View File

@ -13,3 +13,5 @@ vendor
servers servers
dev dev
!build/alpine-linux-amd64 !build/alpine-linux-amd64
config.yml
statup.db

View File

@ -38,27 +38,27 @@ var (
type Notification struct { type Notification struct {
Id int64 `gorm:"primary_key;column:id" json:"id"` Id int64 `gorm:"primary_key;column:id" json:"id"`
Method string `gorm:"column:method" json:"method"` Method string `gorm:"column:method" json:"method"`
Host string `gorm:"not null;column:host" json:"-"` Host string `gorm:"not null;column:host" json:"host,omitempty"`
Port int `gorm:"not null;column:port" json:"-"` Port int `gorm:"not null;column:port" json:"port,omitempty"`
Username string `gorm:"not null;column:username" json:"-"` Username string `gorm:"not null;column:username" json:"username,omitempty"`
Password string `gorm:"not null;column:password" json:"-"` Password string `gorm:"not null;column:password" json:"password,omitempty"`
Var1 string `gorm:"not null;column:var1" json:"-"` Var1 string `gorm:"not null;column:var1" json:"var1,omitempty"`
Var2 string `gorm:"not null;column:var2" json:"-"` Var2 string `gorm:"not null;column:var2" json:"var2,omitempty"`
ApiKey string `gorm:"not null;column:api_key" json:"-"` ApiKey string `gorm:"not null;column:api_key" json:"api_key,omitempty"`
ApiSecret string `gorm:"not null;column:api_secret" json:"-"` ApiSecret string `gorm:"not null;column:api_secret" json:"api_secret,omitempty"`
Enabled bool `gorm:"column:enabled;type:boolean;default:false" json:"enabled"` Enabled bool `gorm:"column:enabled;type:boolean;default:false" json:"enabled"`
Limits int `gorm:"not null;column:limits" json:"-"` 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"`
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
Form []NotificationForm `gorm:"-" json:"-"` Form []NotificationForm `gorm:"-" json:"form"`
logs []*NotificationLog `gorm:"-" json:"-"` logs []*NotificationLog `gorm:"-" json:"-"`
Title string `gorm:"-" json:"-"` Title string `gorm:"-" json:"title"`
Description string `gorm:"-" json:"-"` Description string `gorm:"-" json:"description"`
Author string `gorm:"-" json:"-"` Author string `gorm:"-" json:"-"`
AuthorUrl string `gorm:"-" json:"-"` AuthorUrl string `gorm:"-" json:"-"`
Icon string `gorm:"-" json:"-"` Icon string `gorm:"-" json:"-"`
Delay time.Duration `gorm:"-" json:"-"` Delay time.Duration `gorm:"-" json:"delay"`
Queue []*QueueData `gorm:"-" json:"-"` Queue []*QueueData `gorm:"-" json:"-"`
Running chan bool `gorm:"-" json:"-"` Running chan bool `gorm:"-" json:"-"`
Online bool `gorm:"-" json:"-"` Online bool `gorm:"-" json:"-"`

View File

@ -450,6 +450,94 @@
} }
} }
] ]
},
{
"name": "Notifiers",
"item": [
{
"name": "View Notifier",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "",
"type": "string"
}
]
},
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "{{endpoint}}/api/notifier/mobile",
"host": [
"{{endpoint}}"
],
"path": [
"api",
"notifier",
"mobile"
]
}
},
"response": []
},
{
"name": "Update Notifier",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "",
"type": "string"
}
]
},
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"method\": \"mobile\",\n \"host\": \"\",\n \"port\": 0,\n \"username\": \"\",\n \"var1\": \"ExponentPushToken[XmsQVgIxjgaMKCP5MBoOp9]\",\n \"var2\": \"\",\n \"api_key\": \"\",\n \"api_secret\": \"\",\n \"enabled\": true,\n \"limits\": 3\n}"
},
"url": {
"raw": "{{endpoint}}/api/notifier/mobile",
"host": [
"{{endpoint}}"
],
"path": [
"api",
"notifier",
"mobile"
]
}
},
"response": []
}
],
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "",
"type": "string"
}
]
}
} }
] ]
} }

View File

@ -335,15 +335,49 @@ func apiNotifierGetHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
vars := mux.Vars(r) vars := mux.Vars(r)
notify, notifierObj, err := notifier.SelectNotifier(vars["notifier"]) _, notifierObj, 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
} }
fmt.Println(notify)
fmt.Println(notifierObj)
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(notify) json.NewEncoder(w).Encode(notifierObj.Select())
}
func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
vars := mux.Vars(r)
var notification *notifier.Notification
decoder := json.NewDecoder(r.Body)
decoder.Decode(&notification)
notif, 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
_, err = notifier.Update(not, notif)
if err != nil {
utils.Log(3, fmt.Sprintf("issue updating notifier: %v", err))
}
notifier.OnSave(notif.Method)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(notif)
} }
func isAPIAuthorized(r *http.Request) bool { func isAPIAuthorized(r *http.Request) bool {

View File

@ -116,7 +116,7 @@ func Router() *mux.Router {
// API Notifier Routes // API Notifier Routes
r.Handle("/api/notifier/{notifier}", http.HandlerFunc(apiNotifierGetHandler)).Methods("GET") r.Handle("/api/notifier/{notifier}", http.HandlerFunc(apiNotifierGetHandler)).Methods("GET")
r.Handle("/api/notifier/{notifier}", http.HandlerFunc(apiNotifierGetHandler)).Methods("POST") r.Handle("/api/notifier/{notifier}", http.HandlerFunc(apiNotifierUpdateHandler)).Methods("POST")
r.Handle("/metrics", http.HandlerFunc(prometheusHandler)) r.Handle("/metrics", http.HandlerFunc(prometheusHandler))
r.Handle("/health", http.HandlerFunc(healthCheckHandler)) r.Handle("/health", http.HandlerFunc(healthCheckHandler))

View File

@ -86,9 +86,9 @@ func (u *mobilePush) OnSuccess(s *types.Service) {
// OnSave triggers when this notifier has been saved // OnSave triggers when this notifier has been saved
func (u *mobilePush) OnSave() error { func (u *mobilePush) OnSave() error {
msg := &expo.PushMessage{ msg := &expo.PushMessage{
Body: "This is a test notification", Body: "The Mobile Notifier has been saved",
Sound: "default", Sound: "default",
Title: "Notification Test", Title: "Notification Saved",
Priority: expo.DefaultPriority, Priority: expo.DefaultPriority,
} }
u.AddQueue(0, msg) u.AddQueue(0, msg)

View File

@ -184,6 +184,7 @@ func DeleteAllAssets(folder string) error {
return err return err
} }
// CopyAllToPublic will copy all the files in a rice box into a local folder
func CopyAllToPublic(box *rice.Box, folder string) error { func CopyAllToPublic(box *rice.Box, folder string) error {
err := box.Walk("/", func(path string, info os.FileInfo, err error) error { err := box.Walk("/", func(path string, info os.FileInfo, err error) error {
if info.Name() == "" { if info.Name() == "" {