mirror of https://github.com/statping/statping
Updated Notifier Example (markdown)
parent
ac23e04a5d
commit
b58656000f
|
@ -4,26 +4,30 @@ Below is a full example of a Statup notifier which will give you a good example
|
||||||
package notifiers
|
package notifiers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"github.com/hunterlong/statup/types"
|
"github.com/hunterlong/statup/types"
|
||||||
"github.com/hunterlong/statup/core/notifier"
|
"github.com/hunterlong/statup/core/notifier"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Example struct {
|
type Example struct {
|
||||||
*notifier.Notification // create a new struct using *Notification
|
*notifier.Notification
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
EXAMPLE_METHOD = "example" // give your notifier a unique name (no spaces)
|
|
||||||
)
|
|
||||||
|
|
||||||
var example = &Example{¬ifier.Notification{
|
var example = &Example{¬ifier.Notification{
|
||||||
Method: EXAMPLE_METHOD,
|
Method: METHOD,
|
||||||
Host: "http://exmaplehost.com",
|
Title: "Example",
|
||||||
|
Description: "Example Notifier",
|
||||||
|
Author: "Hunter Long",
|
||||||
|
AuthorUrl: "https://github.com/hunterlong",
|
||||||
|
Delay: time.Duration(5 * time.Second),
|
||||||
Form: []notifier.NotificationForm{{
|
Form: []notifier.NotificationForm{{
|
||||||
Type: "text",
|
Type: "text",
|
||||||
Title: "Host",
|
Title: "Host",
|
||||||
Placeholder: "Insert your Host here.",
|
Placeholder: "Insert your Host here.",
|
||||||
DbField: "host",
|
DbField: "host",
|
||||||
|
SmallText: "this is where you would put the host",
|
||||||
}, {
|
}, {
|
||||||
Type: "text",
|
Type: "text",
|
||||||
Title: "Username",
|
Title: "Username",
|
||||||
|
@ -59,36 +63,103 @@ var example = &Example{¬ifier.Notification{
|
||||||
Title: "Var2",
|
Title: "Var2",
|
||||||
Placeholder: "Var2 goes here",
|
Placeholder: "Var2 goes here",
|
||||||
DbField: "var2",
|
DbField: "var2",
|
||||||
}}},
|
}},
|
||||||
}
|
}}
|
||||||
|
|
||||||
// AddNotifier(example) inside init() is required!
|
// REQUIRED init() will install/load the notifier
|
||||||
func init() {
|
func init() {
|
||||||
notifier.AddNotifier(example)
|
notifier.AddNotifier(example)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run is required for the Notifier interface
|
// REQUIRED - Send is where you would put the action's of your notifier
|
||||||
func (n *Example) Run() error {
|
func (n *Example) Send(msg interface{}) error {
|
||||||
// this function can be a go routine if you need it
|
message := msg.(string)
|
||||||
|
fmt.Printf("i received this string: %v\n", message)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnSave is required for the Notifier interface
|
// REQUIRED
|
||||||
func (n *Example) OnSave() error {
|
func (n *Example) Select() *notifier.Notification {
|
||||||
// this function is triggered when the notifier's form is saved
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Select is required for the Notifier interface
|
|
||||||
func (n *Example) Select() *Notification {
|
|
||||||
return n.Notification
|
return n.Notification
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Example) OnSuccess(s *types.Service) {
|
// REQUIRED
|
||||||
// When a service is successful this function will be triggered!
|
func (n *Example) OnSave() error {
|
||||||
|
msg := fmt.Sprintf("received on save trigger")
|
||||||
|
n.AddQueue(msg)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// REQUIRED
|
||||||
|
func (n *Example) Test() error {
|
||||||
|
msg := fmt.Sprintf("received a test trigger\n")
|
||||||
|
n.AddQueue(msg)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// REQUIRED - BASIC EVENT
|
||||||
|
func (n *Example) OnSuccess(s *types.Service) {
|
||||||
|
msg := fmt.Sprintf("received a count trigger for service: %v\n", s.Name)
|
||||||
|
n.AddQueue(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// REQUIRED - BASIC EVENT
|
||||||
func (n *Example) OnFailure(s *types.Service, f *types.Failure) {
|
func (n *Example) OnFailure(s *types.Service, f *types.Failure) {
|
||||||
// When a service is failing this function will be triggered!
|
msg := fmt.Sprintf("received a failure trigger for service: %v\n", s.Name)
|
||||||
|
n.AddQueue(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OPTIONAL
|
||||||
|
func (n *Example) OnNewService(s *types.Service) {
|
||||||
|
msg := fmt.Sprintf("received a new service trigger for service: %v\n", s.Name)
|
||||||
|
n.AddQueue(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OPTIONAL
|
||||||
|
func (n *Example) OnUpdatedService(s *types.Service) {
|
||||||
|
msg := fmt.Sprintf("received a update service trigger for service: %v\n", s.Name)
|
||||||
|
n.AddQueue(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OPTIONAL
|
||||||
|
func (n *Example) OnDeletedService(s *types.Service) {
|
||||||
|
msg := fmt.Sprintf("received a delete service trigger for service: %v\n", s.Name)
|
||||||
|
n.AddQueue(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OPTIONAL
|
||||||
|
func (n *Example) OnNewUser(s *types.User) {
|
||||||
|
msg := fmt.Sprintf("received a new user trigger for user: %v\n", s.Username)
|
||||||
|
n.AddQueue(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OPTIONAL
|
||||||
|
func (n *Example) OnUpdatedUser(s *types.User) {
|
||||||
|
msg := fmt.Sprintf("received a updated user trigger for user: %v\n", s.Username)
|
||||||
|
n.AddQueue(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OPTIONAL
|
||||||
|
func (n *Example) OnDeletedUser(s *types.User) {
|
||||||
|
msg := fmt.Sprintf("received a deleted user trigger for user: %v\n", s.Username)
|
||||||
|
n.AddQueue(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OPTIONAL
|
||||||
|
func (n *Example) OnUpdatedCore(s *types.Core) {
|
||||||
|
msg := fmt.Sprintf("received a updated core trigger for core: %v\n", s.Name)
|
||||||
|
n.AddQueue(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OPTIONAL
|
||||||
|
func (n *Example) OnNewNotifier(s *Notification) {
|
||||||
|
msg := fmt.Sprintf("received a new notifier trigger for notifier: %v\n", s.Method)
|
||||||
|
n.AddQueue(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OPTIONAL
|
||||||
|
func (n *Example) OnUpdatedNotifier(s *Notification) {
|
||||||
|
msg := fmt.Sprintf("received a update notifier trigger for notifier: %v\n", s.Method)
|
||||||
|
n.AddQueue(msg)
|
||||||
}
|
}
|
||||||
```
|
```
|
Loading…
Reference in New Issue