diff --git a/.gitattributes b/.gitattributes index b552774f..37e6bb8e 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,5 @@ -*.go -text diff=golang -*.gohtml -text diff=golang +*.gohtml linguist-language=golang +*.js linguist-detectable=false +*.yml linguist-detectable=false +*.json linguist-detectable=false +dev/* linguist-vendored diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 00000000..062927ab --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,2 @@ +github: hunterlong +custom: ['https://www.buymeacoffee.com/hunterlong'] diff --git a/Makefile b/Makefile index 05a1e1e8..6a6a7c29 100644 --- a/Makefile +++ b/Makefile @@ -6,14 +6,14 @@ GOCMD=go GOBUILD=$(GOCMD) build -a GOTEST=$(GOCMD) test GOGET=$(GOCMD) get -GOVERSION=1.12.x +GOVERSION=1.13.1 GOINSTALL=$(GOCMD) install XGO=GOPATH=$(GOPATH) xgo -go $(GOVERSION) --dest=build BUILDVERSION=-ldflags "-X main.VERSION=${VERSION} -X main.COMMIT=$(TRAVIS_COMMIT)" RICE=$(GOPATH)/bin/rice PATH:=/usr/local/bin:$(GOPATH)/bin:$(PATH) PUBLISH_BODY='{ "request": { "branch": "master", "message": "Homebrew update version v${VERSION}", "config": { "env": { "VERSION": "${VERSION}", "COMMIT": "$(TRAVIS_COMMIT)" } } } }' -TRAVIS_BUILD_CMD='{ "request": { "branch": "master", "message": "Compile master for Statping v${VERSION}", "config": { "os": [ "linux" ], "language": "go", "go": [ "${GOVERSION}" ], "go_import_path": "github.com/hunterlong/statping", "install": true, "sudo": "required", "services": [ "docker" ], "env": { "VERSION": "${VERSION}" }, "matrix": { "allow_failures": [ { "go": "master" } ], "fast_finish": true }, "before_deploy": [ "git config --local user.name \"hunterlong\"", "git config --local user.email \"info@socialeck.com\"", "git tag v$(VERSION) --force"], "deploy": [ { "provider": "releases", "api_key": "$(GH_TOKEN)", "file_glob": true, "file": "build/*", "skip_cleanup": true } ], "notifications": { "email": false }, "before_script": ["gem install sass"], "script": [ "wget -O statping.gpg $(SIGN_URL)", "gpg --import statping.gpg", "travis_wait 30 docker pull karalabe/xgo-latest", "make release" ], "after_success": [], "after_deploy": [ "make publish-homebrew" ] } } }' +TRAVIS_BUILD_CMD='{ "request": { "branch": "master", "message": "Compile master for Statping v${VERSION}", "config": { "os": [ "linux" ], "language": "go", "go": [ "${GOVERSION}" ], "go_import_path": "github.com/hunterlong/statping", "install": true, "sudo": "required", "services": [ "docker" ], "env": { "VERSION": "${VERSION}" }, "matrix": { "allow_failures": [ { "go": "master" } ], "fast_finish": true }, "before_deploy": [ "git config --local user.name \"hunterlong\"", "git config --local user.email \"info@socialeck.com\"", "git tag v$(VERSION) --force"], "deploy": [ { "provider": "releases", "api_key": "$(GH_TOKEN)", "file_glob": true, "file": "build/*", "skip_cleanup": true } ], "notifications": { "email": false }, "before_script": ["gem install sass"], "script": [ "wget -O statping.gpg $(SIGN_URL)", "gpg --import statping.gpg", "travis_wait 30 docker pull crazymax/xgo:$(GOVERSION)", "make release" ], "after_success": [], "after_deploy": [ "make publish-homebrew" ] } } }' TEST_DIR=$(GOPATH)/src/github.com/hunterlong/statping PATH:=$(PATH) @@ -220,7 +220,7 @@ dev-deps: $(GOINSTALL) github.com/mattn/goveralls $(GOGET) github.com/rendon/testcli $(GOGET) github.com/robertkrimen/godocdown/godocdown - $(GOGET) github.com/karalabe/xgo + $(GOGET) github.com/crazy-max/xgo $(GOGET) github.com/GeertJohan/go.rice $(GOGET) github.com/GeertJohan/go.rice/rice $(GOINSTALL) github.com/GeertJohan/go.rice/rice @@ -344,8 +344,8 @@ valid-sign: # install xgo and pull the xgo docker image xgo-install: clean - go get github.com/karalabe/xgo - docker pull karalabe/xgo-latest + go get github.com/crazy-max/xgo + docker pull crazy-max/xgo:${GOVERSION} heroku: git push heroku master diff --git a/cmd/main.go b/cmd/main.go index 8c00de1a..b0990e07 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -20,7 +20,6 @@ import ( "fmt" "github.com/hunterlong/statping/core" "github.com/hunterlong/statping/handlers" - _ "github.com/hunterlong/statping/notifiers" "github.com/hunterlong/statping/plugin" "github.com/hunterlong/statping/source" "github.com/hunterlong/statping/utils" @@ -119,11 +118,3 @@ func mainProcess() { os.Exit(1) } } - -func ForEachPlugin() { - if len(core.CoreApp.Plugins) > 0 { - //for _, p := range core.Plugins { - // p.OnShutdown() - //} - } -} diff --git a/core/core.go b/core/core.go index 2253ee4a..4ab3f16c 100644 --- a/core/core.go +++ b/core/core.go @@ -19,6 +19,7 @@ import ( "errors" "fmt" "github.com/hunterlong/statping/core/notifier" + "github.com/hunterlong/statping/notifiers" "github.com/hunterlong/statping/source" "github.com/hunterlong/statping/types" "github.com/hunterlong/statping/utils" @@ -64,7 +65,8 @@ func InitApp() { InsertNotifierDB() CoreApp.SelectAllServices(true) checkServices() - CoreApp.Notifications = notifier.Load() + AttachNotifiers() + CoreApp.Notifications = notifier.AllCommunications go DatabaseMaintence() } @@ -177,6 +179,21 @@ func GetLocalIP() string { return "http://localhost" } +// AttachNotifiers will attach all the notifier's into the system +func AttachNotifiers() error { + return notifier.AddNotifiers( + notifiers.Command, + notifiers.Discorder, + notifiers.Emailer, + notifiers.LineNotify, + notifiers.Mobile, + notifiers.Slacker, + notifiers.Telegram, + notifiers.Twilio, + notifiers.Webhook, + ) +} + // ServiceOrder will reorder the services based on 'order_id' (Order) type ServiceOrder []types.ServiceInterface diff --git a/core/notifier/audit.go b/core/notifier/audit.go index 512f3ad5..f1255cc4 100644 --- a/core/notifier/audit.go +++ b/core/notifier/audit.go @@ -25,7 +25,7 @@ var ( ) func checkNotifierForm(n Notifier) error { - notifier := asNotification(n) + notifier := n.Select() for _, f := range notifier.Form { contains := contains(f.DbField, allowedVars) if !contains { diff --git a/core/notifier/events.go b/core/notifier/events.go index fc55d1b4..34900aa4 100644 --- a/core/notifier/events.go +++ b/core/notifier/events.go @@ -54,7 +54,7 @@ sendMessages: for _, comm := range AllCommunications { if isType(comm, new(BasicEvents)) && isEnabled(comm) && (s.Online || inLimits(comm)) { notifier := comm.(Notifier).Select() - utils.Log(1, fmt.Sprintf("Sending failure %v notification for service %v", notifier.Method, s.Name)) + utils.Log(1, fmt.Sprintf("Sending [OnFailure] '%v' notification for service %v", notifier.Method, s.Name)) comm.(BasicEvents).OnFailure(s, f) } } @@ -74,7 +74,7 @@ func OnSuccess(s *types.Service) { for _, comm := range AllCommunications { if isType(comm, new(BasicEvents)) && isEnabled(comm) && (!s.Online || inLimits(comm)) { notifier := comm.(Notifier).Select() - utils.Log(1, fmt.Sprintf("Sending successful %v notification for service %v", notifier.Method, s.Name)) + utils.Log(1, fmt.Sprintf("Sending [OnSuccess] '%v' notification for service %v", notifier.Method, s.Name)) comm.(BasicEvents).OnSuccess(s) } } diff --git a/core/notifier/example_test.go b/core/notifier/example_test.go index 1d46b1e8..9fbb231a 100644 --- a/core/notifier/example_test.go +++ b/core/notifier/example_test.go @@ -18,7 +18,9 @@ package notifier import ( "errors" "fmt" + "github.com/hunterlong/statping/source" "github.com/hunterlong/statping/types" + "github.com/hunterlong/statping/utils" "time" ) @@ -83,7 +85,11 @@ var example = &ExampleNotifier{&Notification{ // init will be ran when Statping is loaded, AddNotifier will add the notifier instance to the system func init() { - AddNotifier(example) + dir = utils.Directory + source.Assets() + utils.InitLogs() + injectDatabase() + AddNotifiers(example) } // Send is the main function to hold your notifier functionality @@ -209,14 +215,14 @@ func ExampleNotification() { }} // AddNotifier accepts a Notifier to load into the Statping Notification system - err := AddNotifier(example) + err := AddNotifiers(example) fmt.Println(err) // Output: } // Add a Notifier to the AddQueue function to insert it into the system func ExampleAddNotifier() { - err := AddNotifier(example) + err := AddNotifiers(example) fmt.Println(err) // Output: } @@ -226,7 +232,9 @@ func ExampleNotification_OnSuccess() { msg := fmt.Sprintf("this is a successful message as a string passing into AddQueue function") example.AddQueue("example", msg) fmt.Println(len(example.Queue)) - // Output: 1 + // Output: + // Notifier 'Example' added new item (example) to the queue. (1 queued) + // 1 } // Add a new message into the queue OnSuccess @@ -252,7 +260,7 @@ func ExampleOnTest() { func ExampleNotification_CanTest() { testable := example.CanTest() fmt.Print(testable) - // Output: false + // Output: true } // Add any type of interface to the AddQueue function to be ran in the queue @@ -261,7 +269,9 @@ func ExampleNotification_AddQueue() { example.AddQueue("example", msg) queue := example.Queue fmt.Printf("Example has %v items in the queue", len(queue)) - // Output: Example has 2 items in the queue + // Output: + // Notifier 'Example' added new item (example) to the queue. (2 queued) + // Example has 2 items in the queue } // The Send method will run the main functionality of your notifier diff --git a/core/notifier/notifiers.go b/core/notifier/notifiers.go index f0924fa2..7d97d4dd 100644 --- a/core/notifier/notifiers.go +++ b/core/notifier/notifiers.go @@ -102,6 +102,7 @@ func (n *Notification) AfterFind() (err error) { func (n *Notification) AddQueue(uid string, msg interface{}) { data := &QueueData{uid, msg} n.Queue = append(n.Queue, data) + utils.Log(0, fmt.Sprintf("Notifier '%v' added new item (%v) to the queue. (%v queued)", n.Method, uid, len(n.Queue))) } // CanTest returns true if the notifier implements the OnTest interface @@ -126,29 +127,21 @@ func asNotification(n Notifier) *Notification { } // AddNotifier accept a Notifier interface to be added into the array -func AddNotifier(n Notifier) error { - if isType(n, new(Notifier)) { - err := checkNotifierForm(n) - if err != nil { - return err +func AddNotifiers(notifiers ...Notifier) error { + for _, n := range notifiers { + if isType(n, new(Notifier)) { + err := checkNotifierForm(n) + if err != nil { + return err + } + AllCommunications = append(AllCommunications, n) + Init(n) + } else { + return errors.New("notifier does not have the required methods") } - AllCommunications = append(AllCommunications, n) - } else { - return errors.New("notifier does not have the required methods") - } - return nil -} - -// Load is called by core to add all the notifier into memory -func Load() []types.AllNotifiers { - var notifiers []types.AllNotifiers - for _, comm := range AllCommunications { - n := comm.(Notifier) - Init(n) - notifiers = append(notifiers, n) } startAllNotifiers() - return notifiers + return nil } // normalizeType will accept multiple interfaces and converts it into a string for logging @@ -179,7 +172,6 @@ func (n *Notification) makeLog(msg interface{}) { Time: utils.Timestamp(time.Now()), Timestamp: time.Now(), } - utils.Log(1, fmt.Sprintf("Notifier %v has sent a message %v", n.Method, log.Message)) n.logs = append(n.logs, log) } @@ -297,7 +289,9 @@ CheckNotifier: msg := notification.Queue[0] err := n.Send(msg.Data) if err != nil { - utils.Log(2, fmt.Sprintf("notifier %v had an error: %v", notification.Method, err)) + utils.Log(2, fmt.Sprintf("Notifier '%v' had an error: %v", notification.Method, err)) + } else { + utils.Log(1, fmt.Sprintf("Notifier '%v' sent outgoing message (%v) %v left in queue.", notification.Method, msg.Id, len(notification.Queue))) } notification.makeLog(msg.Data) if len(notification.Queue) > 1 { diff --git a/core/notifier/notifiers_test.go b/core/notifier/notifiers_test.go index c8686395..d6a2d8e9 100644 --- a/core/notifier/notifiers_test.go +++ b/core/notifier/notifiers_test.go @@ -17,7 +17,6 @@ package notifier import ( "fmt" - "github.com/hunterlong/statping/source" "github.com/hunterlong/statping/types" "github.com/hunterlong/statping/utils" "github.com/jinzhu/gorm" @@ -56,13 +55,6 @@ var core = &types.Core{ Name: "testing notifiers", } -func init() { - dir = utils.Directory - source.Assets() - utils.InitLogs() - injectDatabase() -} - func injectDatabase() { utils.DeleteFile(dir + "/statup.db") db, _ = gorm.Open("sqlite3", dir+"/statup.db") @@ -79,11 +71,6 @@ func TestIsBasicType(t *testing.T) { assert.True(t, isType(example, new(Tester))) } -func TestLoad(t *testing.T) { - notifiers := Load() - assert.Equal(t, 1, len(notifiers)) -} - func TestIsInDatabase(t *testing.T) { in := isInDatabase(example.Notification) assert.True(t, in) diff --git a/notifiers/command.go b/notifiers/command.go index 6216e794..2a1fe232 100644 --- a/notifiers/command.go +++ b/notifiers/command.go @@ -27,10 +27,10 @@ type commandLine struct { *notifier.Notification } -var command = &commandLine{¬ifier.Notification{ - Method: "command", +var Command = &commandLine{¬ifier.Notification{ + Method: "Command", Title: "Shell Command", - Description: "Shell Command allows you to run a customized shell/bash command on the local machine it's running on.", + Description: "Shell Command allows you to run a customized shell/bash Command on the local machine it's running on.", Author: "Hunter Long", AuthorUrl: "https://github.com/hunterlong", Delay: time.Duration(1 * time.Second), @@ -47,24 +47,16 @@ var command = &commandLine{¬ifier.Notification{ Title: "Command to Run on OnSuccess", Placeholder: "curl google.com", DbField: "var1", - SmallText: "This command will run every time a service is receiving a Successful event.", + SmallText: "This Command will run every time a service is receiving a Successful event.", }, { Type: "text", Title: "Command to Run on OnFailure", Placeholder: "curl offline.com", DbField: "var2", - SmallText: "This command will run every time a service is receiving a Failing event.", + SmallText: "This Command will run every time a service is receiving a Failing event.", }}}, } -// init the command notifier -func init() { - err := notifier.AddNotifier(command) - if err != nil { - panic(err) - } -} - func runCommand(app, cmd string) (string, string, error) { outStr, errStr, err := utils.Command(cmd) return outStr, errStr, err @@ -102,7 +94,7 @@ func (u *commandLine) OnTest() error { return err } -// Send for commandLine will send message to expo command push notifications endpoint +// Send for commandLine will send message to expo Command push notifications endpoint func (u *commandLine) Send(msg interface{}) error { cmd := msg.(string) _, _, err := runCommand(u.Host, cmd) diff --git a/notifiers/command_test.go b/notifiers/command_test.go index f6ed7314..36ad0aa5 100644 --- a/notifiers/command_test.go +++ b/notifiers/command_test.go @@ -28,79 +28,75 @@ const ( func TestCommandNotifier(t *testing.T) { t.Parallel() - command.Host = "sh" - command.Var1 = commandTest - command.Var2 = commandTest + Command.Host = "sh" + Command.Var1 = commandTest + Command.Var2 = commandTest currentCount = CountNotifiers() - t.Run("Load command", func(t *testing.T) { - command.Host = "sh" - command.Var1 = commandTest - command.Var2 = commandTest - command.Delay = time.Duration(100 * time.Millisecond) - command.Limits = 99 - err := notifier.AddNotifier(command) + t.Run("Load Command", func(t *testing.T) { + Command.Host = "sh" + Command.Var1 = commandTest + Command.Var2 = commandTest + Command.Delay = time.Duration(100 * time.Millisecond) + Command.Limits = 99 + err := notifier.AddNotifiers(Command) assert.Nil(t, err) - assert.Equal(t, "Hunter Long", command.Author) - assert.Equal(t, "sh", command.Host) - assert.Equal(t, commandTest, command.Var1) - assert.Equal(t, commandTest, command.Var2) + assert.Equal(t, "Hunter Long", Command.Author) + assert.Equal(t, "sh", Command.Host) + assert.Equal(t, commandTest, Command.Var1) + assert.Equal(t, commandTest, Command.Var2) }) - t.Run("Load command Notifier", func(t *testing.T) { - notifier.Load() + t.Run("Command Notifier Tester", func(t *testing.T) { + assert.True(t, Command.CanTest()) }) - t.Run("command Notifier Tester", func(t *testing.T) { - assert.True(t, command.CanTest()) - }) - - t.Run("command Within Limits", func(t *testing.T) { - ok, err := command.WithinLimits() + t.Run("Command Within Limits", func(t *testing.T) { + ok, err := Command.WithinLimits() assert.Nil(t, err) assert.True(t, ok) }) - t.Run("command OnFailure", func(t *testing.T) { - command.OnFailure(TestService, TestFailure) - assert.Equal(t, 1, len(command.Queue)) + t.Run("Command OnFailure", func(t *testing.T) { + Command.OnFailure(TestService, TestFailure) + assert.Equal(t, 1, len(Command.Queue)) }) - t.Run("command OnSuccess", func(t *testing.T) { - command.OnSuccess(TestService) - assert.Equal(t, 1, len(command.Queue)) + t.Run("Command OnSuccess", func(t *testing.T) { + Command.OnSuccess(TestService) + assert.Equal(t, 1, len(Command.Queue)) }) - t.Run("command OnSuccess Again", func(t *testing.T) { - command.OnSuccess(TestService) - assert.Equal(t, 1, len(command.Queue)) - go notifier.Queue(command) + t.Run("Command OnSuccess Again", func(t *testing.T) { + Command.OnSuccess(TestService) + assert.Equal(t, 1, len(Command.Queue)) + go notifier.Queue(Command) time.Sleep(20 * time.Second) - assert.Equal(t, 0, len(command.Queue)) + assert.Equal(t, 0, len(Command.Queue)) }) - t.Run("command Within Limits again", func(t *testing.T) { - ok, err := command.WithinLimits() + t.Run("Command Within Limits again", func(t *testing.T) { + ok, err := Command.WithinLimits() assert.Nil(t, err) assert.True(t, ok) }) - t.Run("command Send", func(t *testing.T) { - command.Send(commandTest) - assert.Equal(t, 0, len(command.Queue)) + t.Run("Command Send", func(t *testing.T) { + Command.Send(commandTest) + assert.Equal(t, 0, len(Command.Queue)) }) - t.Run("command Test", func(t *testing.T) { - command.OnTest() + t.Run("Command Test", func(t *testing.T) { + Command.OnTest() }) - t.Run("command Queue", func(t *testing.T) { - go notifier.Queue(command) + t.Run("Command Queue", func(t *testing.T) { + go notifier.Queue(Command) time.Sleep(5 * time.Second) - assert.Equal(t, "sh", command.Host) - assert.Equal(t, commandTest, command.Var1) - assert.Equal(t, commandTest, command.Var2) - assert.Equal(t, 0, len(command.Queue)) + assert.Equal(t, "sh", Command.Host) + assert.Equal(t, commandTest, Command.Var1) + assert.Equal(t, commandTest, Command.Var2) + assert.Equal(t, 0, len(Command.Queue)) }) } diff --git a/notifiers/discord.go b/notifiers/discord.go index 2d9c345b..d6a37d0a 100644 --- a/notifiers/discord.go +++ b/notifiers/discord.go @@ -31,10 +31,10 @@ type discord struct { *notifier.Notification } -var discorder = &discord{¬ifier.Notification{ +var Discorder = &discord{¬ifier.Notification{ Method: "discord", Title: "discord", - Description: "Send notifications to your discord channel using discord webhooks. Insert your discord channel webhook URL to receive notifications. Based on the discord webhooker API.", + Description: "Send notifications to your discord channel using discord webhooks. Insert your discord channel Webhook URL to receive notifications. Based on the discord webhooker API.", Author: "Hunter Long", AuthorUrl: "https://github.com/hunterlong", Delay: time.Duration(5 * time.Second), @@ -43,23 +43,15 @@ var discorder = &discord{¬ifier.Notification{ Form: []notifier.NotificationForm{{ Type: "text", Title: "discord webhooker URL", - Placeholder: "Insert your webhook URL here", + Placeholder: "Insert your Webhook URL here", DbField: "host", }}}, } -// init the discord notifier -func init() { - err := notifier.AddNotifier(discorder) - if err != nil { - panic(err) - } -} - // Send will send a HTTP Post to the discord API. It accepts type: []byte func (u *discord) Send(msg interface{}) error { message := msg.(string) - _, _, err := utils.HttpRequest(discorder.GetValue("host"), "POST", "application/json", nil, strings.NewReader(message), time.Duration(10*time.Second), true) + _, _, err := utils.HttpRequest(Discorder.GetValue("host"), "POST", "application/json", nil, strings.NewReader(message), time.Duration(10*time.Second), true) return err } @@ -98,7 +90,7 @@ func (u *discord) OnSave() error { func (u *discord) OnTest() error { outError := errors.New("Incorrect discord URL, please confirm URL is correct") message := `{"content": "Testing the discord notifier"}` - contents, _, err := utils.HttpRequest(discorder.Host, "POST", "application/json", nil, bytes.NewBuffer([]byte(message)), time.Duration(10*time.Second), true) + contents, _, err := utils.HttpRequest(Discorder.Host, "POST", "application/json", nil, bytes.NewBuffer([]byte(message)), time.Duration(10*time.Second), true) if string(contents) == "" { return nil } diff --git a/notifiers/discord_test.go b/notifiers/discord_test.go index 02e4af9d..faa01336 100644 --- a/notifiers/discord_test.go +++ b/notifiers/discord_test.go @@ -30,7 +30,7 @@ var ( func init() { DISCORD_URL = os.Getenv("DISCORD_URL") - discorder.Host = DISCORD_URL + Discorder.Host = DISCORD_URL } func TestDiscordNotifier(t *testing.T) { @@ -42,36 +42,32 @@ func TestDiscordNotifier(t *testing.T) { currentCount = CountNotifiers() t.Run("Load discord", func(t *testing.T) { - discorder.Host = DISCORD_URL - discorder.Delay = time.Duration(100 * time.Millisecond) - err := notifier.AddNotifier(discorder) + Discorder.Host = DISCORD_URL + Discorder.Delay = time.Duration(100 * time.Millisecond) + err := notifier.AddNotifiers(Discorder) assert.Nil(t, err) - assert.Equal(t, "Hunter Long", discorder.Author) - assert.Equal(t, DISCORD_URL, discorder.Host) - }) - - t.Run("Load discord Notifier", func(t *testing.T) { - notifier.Load() + assert.Equal(t, "Hunter Long", Discorder.Author) + assert.Equal(t, DISCORD_URL, Discorder.Host) }) t.Run("discord Notifier Tester", func(t *testing.T) { - assert.True(t, discorder.CanTest()) + assert.True(t, Discorder.CanTest()) }) t.Run("discord Within Limits", func(t *testing.T) { - ok, err := discorder.WithinLimits() + ok, err := Discorder.WithinLimits() assert.Nil(t, err) assert.True(t, ok) }) t.Run("discord OnFailure", func(t *testing.T) { - discorder.OnFailure(TestService, TestFailure) - assert.Equal(t, 1, len(discorder.Queue)) + Discorder.OnFailure(TestService, TestFailure) + assert.Equal(t, 1, len(Discorder.Queue)) }) t.Run("discord OnSuccess", func(t *testing.T) { - discorder.OnSuccess(TestService) - assert.Equal(t, 1, len(discorder.Queue)) + Discorder.OnSuccess(TestService) + assert.Equal(t, 1, len(Discorder.Queue)) }) t.Run("discord Check Back Online", func(t *testing.T) { @@ -79,25 +75,25 @@ func TestDiscordNotifier(t *testing.T) { }) t.Run("discord OnSuccess Again", func(t *testing.T) { - discorder.OnSuccess(TestService) - assert.Equal(t, 1, len(discorder.Queue)) + Discorder.OnSuccess(TestService) + assert.Equal(t, 1, len(Discorder.Queue)) }) t.Run("discord Send", func(t *testing.T) { - err := discorder.Send(discordMessage) + err := Discorder.Send(discordMessage) assert.Nil(t, err) }) t.Run("discord Test", func(t *testing.T) { - err := discorder.OnTest() + err := Discorder.OnTest() assert.Nil(t, err) }) t.Run("discord Queue", func(t *testing.T) { - go notifier.Queue(discorder) + go notifier.Queue(Discorder) time.Sleep(1 * time.Second) - assert.Equal(t, DISCORD_URL, discorder.Host) - assert.Equal(t, 0, len(discorder.Queue)) + assert.Equal(t, DISCORD_URL, Discorder.Host) + assert.Equal(t, 0, len(Discorder.Queue)) }) } diff --git a/notifiers/email.go b/notifiers/email.go index d57beee1..d23ec134 100644 --- a/notifiers/email.go +++ b/notifiers/email.go @@ -111,7 +111,7 @@ type email struct { *notifier.Notification } -var emailer = &email{¬ifier.Notification{ +var Emailer = &email{¬ifier.Notification{ Method: "email", Title: "email", Description: "Send emails via SMTP when services are online or offline.", @@ -157,13 +157,6 @@ var emailer = &email{¬ifier.Notification{ }}, }} -func init() { - err := notifier.AddNotifier(emailer) - if err != nil { - panic(err) - } -} - // Send will send the SMTP email with your authentication It accepts type: *emailOutgoing func (u *email) Send(msg interface{}) error { email := msg.(*emailOutgoing) @@ -255,7 +248,7 @@ func (u *email) OnTest() error { } func (u *email) dialSend(email *emailOutgoing) error { - mailer = mail.NewDialer(emailer.Host, emailer.Port, emailer.Username, emailer.Password) + mailer = mail.NewDialer(Emailer.Host, Emailer.Port, Emailer.Username, Emailer.Password) emailSource(email) m := mail.NewMessage() // if email setting TLS is Disabled diff --git a/notifiers/email_test.go b/notifiers/email_test.go index 0e7b3284..fb795247 100644 --- a/notifiers/email_test.go +++ b/notifiers/email_test.go @@ -44,12 +44,12 @@ func init() { EMAIL_SEND_TO = os.Getenv("EMAIL_SEND_TO") EMAIL_PORT = utils.ToInt(os.Getenv("EMAIL_PORT")) - emailer.Host = EMAIL_HOST - emailer.Username = EMAIL_USER - emailer.Password = EMAIL_PASS - emailer.Var1 = EMAIL_OUTGOING - emailer.Var2 = EMAIL_SEND_TO - emailer.Port = int(EMAIL_PORT) + Emailer.Host = EMAIL_HOST + Emailer.Username = EMAIL_USER + Emailer.Password = EMAIL_PASS + Emailer.Var1 = EMAIL_OUTGOING + Emailer.Var2 = EMAIL_SEND_TO + Emailer.Port = int(EMAIL_PORT) } func TestEmailNotifier(t *testing.T) { @@ -61,36 +61,32 @@ func TestEmailNotifier(t *testing.T) { currentCount = CountNotifiers() t.Run("New Emailer", func(t *testing.T) { - emailer.Host = EMAIL_HOST - emailer.Username = EMAIL_USER - emailer.Password = EMAIL_PASS - emailer.Var1 = EMAIL_OUTGOING - emailer.Var2 = EMAIL_SEND_TO - emailer.Port = int(EMAIL_PORT) - emailer.Delay = time.Duration(100 * time.Millisecond) + Emailer.Host = EMAIL_HOST + Emailer.Username = EMAIL_USER + Emailer.Password = EMAIL_PASS + Emailer.Var1 = EMAIL_OUTGOING + Emailer.Var2 = EMAIL_SEND_TO + Emailer.Port = int(EMAIL_PORT) + Emailer.Delay = time.Duration(100 * time.Millisecond) testEmail = &emailOutgoing{ - To: emailer.GetValue("var2"), + To: Emailer.GetValue("var2"), Subject: fmt.Sprintf("Service %v is Failing", TestService.Name), Template: mainEmailTemplate, Data: TestService, - From: emailer.GetValue("var1"), + From: Emailer.GetValue("var1"), } }) t.Run("Add email Notifier", func(t *testing.T) { - err := notifier.AddNotifier(emailer) + err := notifier.AddNotifiers(Emailer) assert.Nil(t, err) - assert.Equal(t, "Hunter Long", emailer.Author) - assert.Equal(t, EMAIL_HOST, emailer.Host) - }) - - t.Run("Emailer Load", func(t *testing.T) { - notifier.Load() + assert.Equal(t, "Hunter Long", Emailer.Author) + assert.Equal(t, EMAIL_HOST, Emailer.Host) }) t.Run("email Within Limits", func(t *testing.T) { - ok, err := emailer.WithinLimits() + ok, err := Emailer.WithinLimits() assert.Nil(t, err) assert.True(t, ok) }) @@ -101,13 +97,13 @@ func TestEmailNotifier(t *testing.T) { }) t.Run("email OnFailure", func(t *testing.T) { - emailer.OnFailure(TestService, TestFailure) - assert.Equal(t, 1, len(emailer.Queue)) + Emailer.OnFailure(TestService, TestFailure) + assert.Equal(t, 1, len(Emailer.Queue)) }) t.Run("email OnSuccess", func(t *testing.T) { - emailer.OnSuccess(TestService) - assert.Equal(t, 1, len(emailer.Queue)) + Emailer.OnSuccess(TestService) + assert.Equal(t, 1, len(Emailer.Queue)) }) t.Run("email Check Back Online", func(t *testing.T) { @@ -115,26 +111,26 @@ func TestEmailNotifier(t *testing.T) { }) t.Run("email OnSuccess Again", func(t *testing.T) { - emailer.OnSuccess(TestService) - assert.Equal(t, 1, len(emailer.Queue)) + Emailer.OnSuccess(TestService) + assert.Equal(t, 1, len(Emailer.Queue)) }) t.Run("email Send", func(t *testing.T) { - err := emailer.Send(testEmail) + err := Emailer.Send(testEmail) assert.Nil(t, err) }) - t.Run("emailer Test", func(t *testing.T) { + t.Run("Emailer Test", func(t *testing.T) { t.SkipNow() - err := emailer.OnTest() + err := Emailer.OnTest() assert.Nil(t, err) }) t.Run("email Run Queue", func(t *testing.T) { - go notifier.Queue(emailer) + go notifier.Queue(Emailer) time.Sleep(6 * time.Second) - assert.Equal(t, EMAIL_HOST, emailer.Host) - assert.Equal(t, 0, len(emailer.Queue)) + assert.Equal(t, EMAIL_HOST, Emailer.Host) + assert.Equal(t, 0, len(Emailer.Queue)) }) } diff --git a/notifiers/line_notify.go b/notifiers/line_notify.go index eb6084ee..e3f60cc0 100644 --- a/notifiers/line_notify.go +++ b/notifiers/line_notify.go @@ -33,7 +33,7 @@ type lineNotifier struct { *notifier.Notification } -var lineNotify = &lineNotifier{¬ifier.Notification{ +var LineNotify = &lineNotifier{¬ifier.Notification{ Method: lineNotifyMethod, Title: "LINE Notify", Description: "LINE Notify will send notifications to your LINE Notify account when services are offline or online. Based on the LINE Notify API.", @@ -48,14 +48,6 @@ var lineNotify = &lineNotifier{¬ifier.Notification{ }}}, } -// DEFINE YOUR NOTIFICATION HERE. -func init() { - err := notifier.AddNotifier(lineNotify) - if err != nil { - panic(err) - } -} - // Send will send a HTTP Post with the Authorization to the notify-api.line.me server. It accepts type: string func (u *lineNotifier) Send(msg interface{}) error { message := msg.(string) diff --git a/notifiers/mobile.go b/notifiers/mobile.go index ed238580..277d10f7 100644 --- a/notifiers/mobile.go +++ b/notifiers/mobile.go @@ -31,10 +31,10 @@ type mobilePush struct { *notifier.Notification } -var mobile = &mobilePush{¬ifier.Notification{ +var Mobile = &mobilePush{¬ifier.Notification{ Method: "mobile", Title: "Mobile Notifications", - Description: `Receive push notifications on your mobile device using the Statping App. You can scan the Authentication QR Code found in Settings to get the mobile app setup in seconds. + Description: `Receive push notifications on your Mobile device using the Statping App. You can scan the Authentication QR Code found in Settings to get the Mobile app setup in seconds.

`, Author: "Hunter Long", AuthorUrl: "https://github.com/hunterlong", @@ -43,7 +43,7 @@ var mobile = &mobilePush{¬ifier.Notification{ Form: []notifier.NotificationForm{{ Type: "text", Title: "Device Identifiers", - Placeholder: "A list of your mobile device push notification ID's.", + Placeholder: "A list of your Mobile device push notification ID's.", DbField: "var1", IsHidden: true, }, { @@ -55,14 +55,6 @@ var mobile = &mobilePush{¬ifier.Notification{ }}}, } -// init the discord notifier -func init() { - err := notifier.AddNotifier(mobile) - if err != nil { - panic(err) - } -} - func (u *mobilePush) Select() *notifier.Notification { return u.Notification } diff --git a/notifiers/mobile_test.go b/notifiers/mobile_test.go index 2e56131f..0638d50b 100644 --- a/notifiers/mobile_test.go +++ b/notifiers/mobile_test.go @@ -31,90 +31,79 @@ var ( func init() { MOBILE_ID = os.Getenv("MOBILE_ID") MOBILE_NUMBER = os.Getenv("MOBILE_NUMBER") - mobile.Var1 = MOBILE_ID + Mobile.Var1 = MOBILE_ID } func TestMobileNotifier(t *testing.T) { t.Parallel() - mobile.Var1 = MOBILE_ID - mobile.Var2 = os.Getenv("MOBILE_NUMBER") + Mobile.Var1 = MOBILE_ID + Mobile.Var2 = os.Getenv("MOBILE_NUMBER") if MOBILE_ID == "" { - t.Log("mobile notifier testing skipped, missing MOBILE_ID environment variable") + t.Log("Mobile notifier testing skipped, missing MOBILE_ID environment variable") t.SkipNow() } currentCount = CountNotifiers() - t.Run("Load mobile", func(t *testing.T) { - mobile.Var1 = MOBILE_ID - mobile.Var2 = MOBILE_NUMBER - mobile.Delay = time.Duration(100 * time.Millisecond) - mobile.Limits = 10 - err := notifier.AddNotifier(mobile) + t.Run("Load Mobile", func(t *testing.T) { + Mobile.Var1 = MOBILE_ID + Mobile.Var2 = MOBILE_NUMBER + Mobile.Delay = time.Duration(100 * time.Millisecond) + Mobile.Limits = 10 + err := notifier.AddNotifiers(Mobile) assert.Nil(t, err) - assert.Equal(t, "Hunter Long", mobile.Author) - assert.Equal(t, MOBILE_ID, mobile.Var1) - assert.Equal(t, MOBILE_NUMBER, mobile.Var2) + assert.Equal(t, "Hunter Long", Mobile.Author) + assert.Equal(t, MOBILE_ID, Mobile.Var1) + assert.Equal(t, MOBILE_NUMBER, Mobile.Var2) }) - t.Run("Load mobile Notifier", func(t *testing.T) { - notifier.Load() + t.Run("Mobile Notifier Tester", func(t *testing.T) { + assert.True(t, Mobile.CanTest()) }) - t.Run("mobile Notifier Tester", func(t *testing.T) { - assert.True(t, mobile.CanTest()) - }) - - t.Run("mobile Within Limits", func(t *testing.T) { - ok, err := mobile.WithinLimits() + t.Run("Mobile Within Limits", func(t *testing.T) { + ok, err := Mobile.WithinLimits() assert.Nil(t, err) assert.True(t, ok) }) - t.Run("mobile OnFailure", func(t *testing.T) { - mobile.OnFailure(TestService, TestFailure) - assert.Equal(t, 1, len(mobile.Queue)) + t.Run("Mobile OnFailure", func(t *testing.T) { + Mobile.OnFailure(TestService, TestFailure) + assert.Equal(t, 1, len(Mobile.Queue)) }) - t.Run("mobile OnFailure multiple times", func(t *testing.T) { - for i := 0; i <= 5; i++ { - mobile.OnFailure(TestService, TestFailure) - } - assert.Equal(t, 7, len(mobile.Queue)) + t.Run("Mobile OnSuccess", func(t *testing.T) { + Mobile.OnSuccess(TestService) + assert.Equal(t, 1, len(Mobile.Queue)) }) - t.Run("mobile OnSuccess", func(t *testing.T) { - mobile.OnSuccess(TestService) - assert.Equal(t, 7, len(mobile.Queue)) - }) - - t.Run("mobile OnSuccess Again", func(t *testing.T) { + t.Run("Mobile OnSuccess Again", func(t *testing.T) { t.SkipNow() assert.True(t, TestService.Online) - mobile.OnSuccess(TestService) - assert.Equal(t, 1, len(mobile.Queue)) - go notifier.Queue(mobile) + Mobile.OnSuccess(TestService) + assert.Equal(t, 1, len(Mobile.Queue)) + go notifier.Queue(Mobile) time.Sleep(20 * time.Second) - assert.Equal(t, 1, len(mobile.Queue)) + assert.Equal(t, 1, len(Mobile.Queue)) }) - t.Run("mobile Within Limits again", func(t *testing.T) { - ok, err := mobile.WithinLimits() + t.Run("Mobile Within Limits again", func(t *testing.T) { + ok, err := Mobile.WithinLimits() assert.Nil(t, err) assert.True(t, ok) }) - t.Run("mobile Test", func(t *testing.T) { + t.Run("Mobile Test", func(t *testing.T) { t.SkipNow() - err := mobile.OnTest() + err := Mobile.OnTest() assert.Nil(t, err) }) - t.Run("mobile Queue", func(t *testing.T) { + t.Run("Mobile Queue", func(t *testing.T) { t.SkipNow() - go notifier.Queue(mobile) + go notifier.Queue(Mobile) time.Sleep(15 * time.Second) - assert.Equal(t, MOBILE_ID, mobile.Var1) - assert.Equal(t, 0, len(mobile.Queue)) + assert.Equal(t, MOBILE_ID, Mobile.Var1) + assert.Equal(t, 0, len(Mobile.Queue)) }) } diff --git a/notifiers/slack.go b/notifiers/slack.go index 3819e0e0..5ab9491d 100644 --- a/notifiers/slack.go +++ b/notifiers/slack.go @@ -34,18 +34,11 @@ const ( slackText = `{"text":"{{.}}"}` ) -func init() { - err := notifier.AddNotifier(slacker) - if err != nil { - panic(err) - } -} - type slack struct { *notifier.Notification } -var slacker = &slack{¬ifier.Notification{ +var Slacker = &slack{¬ifier.Notification{ Method: slackMethod, Title: "slack", Description: "Send notifications to your slack channel when a service is offline. Insert your Incoming webhooker URL for your channel to receive notifications. Based on the slack API.", @@ -57,7 +50,7 @@ var slacker = &slack{¬ifier.Notification{ Form: []notifier.NotificationForm{{ Type: "text", Title: "Incoming webhooker Url", - Placeholder: "Insert your slack webhook URL here.", + Placeholder: "Insert your slack Webhook URL here.", SmallText: "Incoming webhooker URL from slack Apps", DbField: "Host", Required: true, @@ -71,7 +64,7 @@ func parseSlackMessage(id int64, temp string, data interface{}) error { if err != nil { return err } - slacker.AddQueue(fmt.Sprintf("service_%v", id), buf.String()) + Slacker.AddQueue(fmt.Sprintf("service_%v", id), buf.String()) return nil } diff --git a/notifiers/slack_test.go b/notifiers/slack_test.go index f5e37175..5a9da0ab 100644 --- a/notifiers/slack_test.go +++ b/notifiers/slack_test.go @@ -30,13 +30,13 @@ var ( func init() { SLACK_URL = os.Getenv("SLACK_URL") - slacker.Host = SLACK_URL + Slacker.Host = SLACK_URL } func TestSlackNotifier(t *testing.T) { t.Parallel() SLACK_URL = os.Getenv("SLACK_URL") - slacker.Host = SLACK_URL + Slacker.Host = SLACK_URL if SLACK_URL == "" { t.Log("slack notifier testing skipped, missing SLACK_URL environment variable") t.SkipNow() @@ -44,76 +44,72 @@ func TestSlackNotifier(t *testing.T) { currentCount = CountNotifiers() t.Run("Load slack", func(t *testing.T) { - slacker.Host = SLACK_URL - slacker.Delay = time.Duration(100 * time.Millisecond) - slacker.Limits = 3 - err := notifier.AddNotifier(slacker) + Slacker.Host = SLACK_URL + Slacker.Delay = time.Duration(100 * time.Millisecond) + Slacker.Limits = 3 + err := notifier.AddNotifiers(Slacker) assert.Nil(t, err) - assert.Equal(t, "Hunter Long", slacker.Author) - assert.Equal(t, SLACK_URL, slacker.Host) - }) - - t.Run("Load slack Notifier", func(t *testing.T) { - notifier.Load() + assert.Equal(t, "Hunter Long", Slacker.Author) + assert.Equal(t, SLACK_URL, Slacker.Host) }) t.Run("slack Notifier Tester", func(t *testing.T) { - assert.True(t, slacker.CanTest()) + assert.True(t, Slacker.CanTest()) }) //t.Run("slack parse message", func(t *testing.T) { // err := parseSlackMessage(slackText, "this is a test!") // assert.Nil(t, err) - // assert.Equal(t, 1, len(slacker.Queue)) + // assert.Equal(t, 1, len(Slacker.Queue)) //}) t.Run("slack Within Limits", func(t *testing.T) { - ok, err := slacker.WithinLimits() + ok, err := Slacker.WithinLimits() assert.Nil(t, err) assert.True(t, ok) }) t.Run("slack OnFailure", func(t *testing.T) { - slacker.OnFailure(TestService, TestFailure) - assert.Equal(t, 1, len(slacker.Queue)) + Slacker.OnFailure(TestService, TestFailure) + assert.Equal(t, 1, len(Slacker.Queue)) }) t.Run("slack OnSuccess", func(t *testing.T) { - slacker.OnSuccess(TestService) - assert.Equal(t, 1, len(slacker.Queue)) + Slacker.OnSuccess(TestService) + assert.Equal(t, 1, len(Slacker.Queue)) }) t.Run("slack OnSuccess Again", func(t *testing.T) { assert.True(t, TestService.Online) - slacker.OnSuccess(TestService) - assert.Equal(t, 1, len(slacker.Queue)) - go notifier.Queue(slacker) + Slacker.OnSuccess(TestService) + assert.Equal(t, 1, len(Slacker.Queue)) + go notifier.Queue(Slacker) time.Sleep(15 * time.Second) - assert.Equal(t, 0, len(slacker.Queue)) + assert.Equal(t, 0, len(Slacker.Queue)) }) t.Run("slack Within Limits again", func(t *testing.T) { - ok, err := slacker.WithinLimits() + ok, err := Slacker.WithinLimits() assert.Nil(t, err) assert.True(t, ok) }) t.Run("slack Send", func(t *testing.T) { - err := slacker.Send(slackTestMessage) + err := Slacker.Send(slackTestMessage) assert.Nil(t, err) - assert.Equal(t, 0, len(slacker.Queue)) + assert.Equal(t, 0, len(Slacker.Queue)) }) t.Run("slack Test", func(t *testing.T) { - err := slacker.OnTest() + err := Slacker.OnTest() assert.Nil(t, err) }) t.Run("slack Queue", func(t *testing.T) { - go notifier.Queue(slacker) + go notifier.Queue(Slacker) time.Sleep(10 * time.Second) - assert.Equal(t, SLACK_URL, slacker.Host) - assert.Equal(t, 0, len(slacker.Queue)) + assert.Equal(t, SLACK_URL, Slacker.Host) + assert.Equal(t, 0, len(Slacker.Queue)) }) } diff --git a/notifiers/telegram.go b/notifiers/telegram.go index 6215cf43..ec8148db 100644 --- a/notifiers/telegram.go +++ b/notifiers/telegram.go @@ -31,7 +31,7 @@ type telegram struct { *notifier.Notification } -var telegramNotifier = &telegram{¬ifier.Notification{ +var Telegram = &telegram{¬ifier.Notification{ Method: "telegram", Title: "Telegram", Description: "Receive notifications on your Telegram channel when a service has an issue. You must get a Telegram API token from the /botfather. Review the Telegram API Tutorial to learn how to generate a new API Token.", @@ -56,14 +56,6 @@ var telegramNotifier = &telegram{¬ifier.Notification{ }}}, } -// DEFINE YOUR NOTIFICATION HERE. -func init() { - err := notifier.AddNotifier(telegramNotifier) - if err != nil { - panic(err) - } -} - func (u *telegram) Select() *notifier.Notification { return u.Notification } diff --git a/notifiers/telegram_test.go b/notifiers/telegram_test.go index 6ed7ff8c..e6ab5f56 100644 --- a/notifiers/telegram_test.go +++ b/notifiers/telegram_test.go @@ -32,8 +32,8 @@ var ( func init() { telegramToken = os.Getenv("TELEGRAM_TOKEN") telegramChannel = os.Getenv("TELEGRAM_CHANNEL") - telegramNotifier.ApiSecret = telegramToken - telegramNotifier.Var1 = telegramChannel + Telegram.ApiSecret = telegramToken + Telegram.Var1 = telegramChannel } func TestTelegramNotifier(t *testing.T) { @@ -46,29 +46,25 @@ func TestTelegramNotifier(t *testing.T) { currentCount = CountNotifiers() t.Run("Load Telegram", func(t *testing.T) { - telegramNotifier.ApiSecret = telegramToken - telegramNotifier.Var1 = telegramChannel - telegramNotifier.Delay = time.Duration(1 * time.Second) - err := notifier.AddNotifier(telegramNotifier) + Telegram.ApiSecret = telegramToken + Telegram.Var1 = telegramChannel + Telegram.Delay = time.Duration(1 * time.Second) + err := notifier.AddNotifiers(Telegram) assert.Nil(t, err) - assert.Equal(t, "Hunter Long", telegramNotifier.Author) - assert.Equal(t, telegramToken, telegramNotifier.ApiSecret) - assert.Equal(t, telegramChannel, telegramNotifier.Var1) - }) - - t.Run("Load Telegram Notifier", func(t *testing.T) { - notifier.Load() + assert.Equal(t, "Hunter Long", Telegram.Author) + assert.Equal(t, telegramToken, Telegram.ApiSecret) + assert.Equal(t, telegramChannel, Telegram.Var1) }) t.Run("Telegram Within Limits", func(t *testing.T) { - ok, err := telegramNotifier.WithinLimits() + ok, err := Telegram.WithinLimits() assert.Nil(t, err) assert.True(t, ok) }) t.Run("Telegram OnFailure", func(t *testing.T) { - telegramNotifier.OnFailure(TestService, TestFailure) - assert.Equal(t, 1, len(telegramNotifier.Queue)) + Telegram.OnFailure(TestService, TestFailure) + assert.Equal(t, 1, len(Telegram.Queue)) }) t.Run("Telegram Check Offline", func(t *testing.T) { @@ -76,8 +72,8 @@ func TestTelegramNotifier(t *testing.T) { }) t.Run("Telegram OnSuccess", func(t *testing.T) { - telegramNotifier.OnSuccess(TestService) - assert.Equal(t, 1, len(telegramNotifier.Queue)) + Telegram.OnSuccess(TestService) + assert.Equal(t, 1, len(Telegram.Queue)) }) t.Run("Telegram Check Back Online", func(t *testing.T) { @@ -85,25 +81,25 @@ func TestTelegramNotifier(t *testing.T) { }) t.Run("Telegram OnSuccess Again", func(t *testing.T) { - telegramNotifier.OnSuccess(TestService) - assert.Equal(t, 1, len(telegramNotifier.Queue)) + Telegram.OnSuccess(TestService) + assert.Equal(t, 1, len(Telegram.Queue)) }) t.Run("Telegram Send", func(t *testing.T) { - err := telegramNotifier.Send(telegramMessage) + err := Telegram.Send(telegramMessage) assert.Nil(t, err) }) t.Run("Telegram Test", func(t *testing.T) { - err := telegramNotifier.OnTest() + err := Telegram.OnTest() assert.Nil(t, err) }) t.Run("Telegram Queue", func(t *testing.T) { - go notifier.Queue(telegramNotifier) + go notifier.Queue(Telegram) time.Sleep(3 * time.Second) - assert.Equal(t, telegramToken, telegramNotifier.ApiSecret) - assert.Equal(t, 0, len(telegramNotifier.Queue)) + assert.Equal(t, telegramToken, Telegram.ApiSecret) + assert.Equal(t, 0, len(Telegram.Queue)) }) } diff --git a/notifiers/twilio.go b/notifiers/twilio.go index e660173d..0f381116 100644 --- a/notifiers/twilio.go +++ b/notifiers/twilio.go @@ -31,7 +31,7 @@ type twilio struct { *notifier.Notification } -var twilioNotifier = &twilio{¬ifier.Notification{ +var Twilio = &twilio{¬ifier.Notification{ Method: "twilio", Title: "Twilio", Description: "Receive SMS text messages directly to your cellphone when a service is offline. You can use a Twilio test account with limits. This notifier uses the Twilio API.", @@ -66,14 +66,6 @@ var twilioNotifier = &twilio{¬ifier.Notification{ }}}, } -// DEFINE YOUR NOTIFICATION HERE. -func init() { - err := notifier.AddNotifier(twilioNotifier) - if err != nil { - panic(err) - } -} - func (u *twilio) Select() *notifier.Notification { return u.Notification } diff --git a/notifiers/twilio_test.go b/notifiers/twilio_test.go index 11e9e47f..0cac1a1f 100644 --- a/notifiers/twilio_test.go +++ b/notifiers/twilio_test.go @@ -37,10 +37,10 @@ func init() { TWILIO_FROM = os.Getenv("TWILIO_FROM") TWILIO_TO = os.Getenv("TWILIO_TO") - twilioNotifier.ApiKey = TWILIO_SID - twilioNotifier.ApiSecret = TWILIO_SECRET - twilioNotifier.Var1 = TWILIO_TO - twilioNotifier.Var2 = TWILIO_FROM + Twilio.ApiKey = TWILIO_SID + Twilio.ApiSecret = TWILIO_SECRET + Twilio.Var1 = TWILIO_TO + Twilio.Var2 = TWILIO_FROM } func TestTwilioNotifier(t *testing.T) { @@ -52,27 +52,23 @@ func TestTwilioNotifier(t *testing.T) { currentCount = CountNotifiers() t.Run("Load Twilio", func(t *testing.T) { - twilioNotifier.ApiKey = TWILIO_SID - twilioNotifier.Delay = time.Duration(100 * time.Millisecond) - err := notifier.AddNotifier(twilioNotifier) + Twilio.ApiKey = TWILIO_SID + Twilio.Delay = time.Duration(100 * time.Millisecond) + err := notifier.AddNotifiers(Twilio) assert.Nil(t, err) - assert.Equal(t, "Hunter Long", twilioNotifier.Author) - assert.Equal(t, TWILIO_SID, twilioNotifier.ApiKey) - }) - - t.Run("Load Twilio Notifier", func(t *testing.T) { - notifier.Load() + assert.Equal(t, "Hunter Long", Twilio.Author) + assert.Equal(t, TWILIO_SID, Twilio.ApiKey) }) t.Run("Twilio Within Limits", func(t *testing.T) { - ok, err := twilioNotifier.WithinLimits() + ok, err := Twilio.WithinLimits() assert.Nil(t, err) assert.True(t, ok) }) t.Run("Twilio OnFailure", func(t *testing.T) { - twilioNotifier.OnFailure(TestService, TestFailure) - assert.Len(t, twilioNotifier.Queue, 1) + Twilio.OnFailure(TestService, TestFailure) + assert.Len(t, Twilio.Queue, 1) }) t.Run("Twilio Check Offline", func(t *testing.T) { @@ -80,8 +76,8 @@ func TestTwilioNotifier(t *testing.T) { }) t.Run("Twilio OnSuccess", func(t *testing.T) { - twilioNotifier.OnSuccess(TestService) - assert.Len(t, twilioNotifier.Queue, 2) + Twilio.OnSuccess(TestService) + assert.Len(t, Twilio.Queue, 2) }) t.Run("Twilio Check Back Online", func(t *testing.T) { @@ -89,25 +85,25 @@ func TestTwilioNotifier(t *testing.T) { }) t.Run("Twilio OnSuccess Again", func(t *testing.T) { - twilioNotifier.OnSuccess(TestService) - assert.Len(t, twilioNotifier.Queue, 2) + Twilio.OnSuccess(TestService) + assert.Len(t, Twilio.Queue, 2) }) t.Run("Twilio Send", func(t *testing.T) { - err := twilioNotifier.Send(twilioMessage) + err := Twilio.Send(twilioMessage) assert.Nil(t, err) }) t.Run("Twilio Test", func(t *testing.T) { - err := twilioNotifier.OnTest() + err := Twilio.OnTest() assert.Nil(t, err) }) t.Run("Twilio Queue", func(t *testing.T) { - go notifier.Queue(twilioNotifier) + go notifier.Queue(Twilio) time.Sleep(1 * time.Second) - assert.Equal(t, TWILIO_SID, twilioNotifier.ApiKey) - assert.Equal(t, 0, len(twilioNotifier.Queue)) + assert.Equal(t, TWILIO_SID, Twilio.ApiKey) + assert.Equal(t, 0, len(Twilio.Queue)) }) } diff --git a/notifiers/webhook.go b/notifiers/webhook.go index d4136ccf..c76b69a9 100644 --- a/notifiers/webhook.go +++ b/notifiers/webhook.go @@ -28,14 +28,14 @@ import ( ) const ( - webhookMethod = "webhook" + webhookMethod = "Webhook" ) type webhooker struct { *notifier.Notification } -var webhook = &webhooker{¬ifier.Notification{ +var Webhook = &webhooker{¬ifier.Notification{ Method: webhookMethod, Title: "HTTP webhooker", Description: "Send a custom HTTP request to a specific URL with your own body, headers, and parameters.", @@ -78,14 +78,6 @@ var webhook = &webhooker{¬ifier.Notification{ }, }}} -// DEFINE YOUR NOTIFICATION HERE. -func init() { - err := notifier.AddNotifier(webhook) - if err != nil { - panic(err) - } -} - // Send will send a HTTP Post to the webhooker API. It accepts type: string func (w *webhooker) Send(msg interface{}) error { resp, err := w.sendHttpWebhook(msg.(string)) @@ -158,7 +150,7 @@ func (w *webhooker) OnTest() error { } defer resp.Body.Close() content, err := ioutil.ReadAll(resp.Body) - utils.Log(1, fmt.Sprintf("webhook notifier received: '%v'", string(content))) + utils.Log(1, fmt.Sprintf("Webhook notifier received: '%v'", string(content))) return err } diff --git a/notifiers/webhook_test.go b/notifiers/webhook_test.go index 3ac57715..a513d755 100644 --- a/notifiers/webhook_test.go +++ b/notifiers/webhook_test.go @@ -30,10 +30,10 @@ var ( ) func init() { - webhook.Host = webhookTestUrl - webhook.Var1 = "POST" - webhook.Var2 = webhookMessage - webhook.ApiKey = "application/json" + Webhook.Host = webhookTestUrl + Webhook.Var1 = "POST" + Webhook.Var2 = webhookMessage + Webhook.ApiKey = "application/json" } func TestWebhookNotifier(t *testing.T) { @@ -41,26 +41,18 @@ func TestWebhookNotifier(t *testing.T) { currentCount = CountNotifiers() t.Run("Load webhooker", func(t *testing.T) { - webhook.Host = webhookTestUrl - webhook.Delay = time.Duration(100 * time.Millisecond) - webhook.ApiKey = apiKey - err := notifier.AddNotifier(webhook) + Webhook.Host = webhookTestUrl + Webhook.Delay = time.Duration(100 * time.Millisecond) + Webhook.ApiKey = apiKey + err := notifier.AddNotifiers(Webhook) assert.Nil(t, err) - assert.Equal(t, "Hunter Long", webhook.Author) - assert.Equal(t, webhookTestUrl, webhook.Host) - assert.Equal(t, apiKey, webhook.ApiKey) - }) - - t.Run("Load webhooker Notifier", func(t *testing.T) { - notifier.Load() - }) - - t.Run("Load webhooker Notifier", func(t *testing.T) { - notifier.Load() + assert.Equal(t, "Hunter Long", Webhook.Author) + assert.Equal(t, webhookTestUrl, Webhook.Host) + assert.Equal(t, apiKey, Webhook.ApiKey) }) t.Run("webhooker Notifier Tester", func(t *testing.T) { - assert.True(t, webhook.CanTest()) + assert.True(t, Webhook.CanTest()) }) t.Run("webhooker Replace Body Text", func(t *testing.T) { @@ -69,19 +61,19 @@ func TestWebhookNotifier(t *testing.T) { }) t.Run("webhooker Within Limits", func(t *testing.T) { - ok, err := webhook.WithinLimits() + ok, err := Webhook.WithinLimits() assert.Nil(t, err) assert.True(t, ok) }) t.Run("webhooker OnFailure", func(t *testing.T) { - webhook.OnFailure(TestService, TestFailure) - assert.Len(t, webhook.Queue, 1) + Webhook.OnFailure(TestService, TestFailure) + assert.Len(t, Webhook.Queue, 1) }) t.Run("webhooker OnSuccess", func(t *testing.T) { - webhook.OnSuccess(TestService) - assert.Equal(t, len(webhook.Queue), 1) + Webhook.OnSuccess(TestService) + assert.Equal(t, len(Webhook.Queue), 1) }) t.Run("webhooker Check Back Online", func(t *testing.T) { @@ -89,21 +81,21 @@ func TestWebhookNotifier(t *testing.T) { }) t.Run("webhooker OnSuccess Again", func(t *testing.T) { - webhook.OnSuccess(TestService) - assert.Equal(t, len(webhook.Queue), 1) + Webhook.OnSuccess(TestService) + assert.Equal(t, len(Webhook.Queue), 1) }) t.Run("webhooker Send", func(t *testing.T) { - err := webhook.Send(fullMsg) + err := Webhook.Send(fullMsg) assert.Nil(t, err) - assert.Equal(t, len(webhook.Queue), 1) + assert.Equal(t, len(Webhook.Queue), 1) }) t.Run("webhooker Queue", func(t *testing.T) { - go notifier.Queue(webhook) + go notifier.Queue(Webhook) time.Sleep(8 * time.Second) - assert.Equal(t, webhookTestUrl, webhook.Host) - assert.Equal(t, len(webhook.Queue), 0) + assert.Equal(t, webhookTestUrl, Webhook.Host) + assert.Equal(t, len(Webhook.Queue), 0) }) } diff --git a/source/wiki.go b/source/wiki.go index c74283d8..9dcc0d82 100644 --- a/source/wiki.go +++ b/source/wiki.go @@ -1,6 +1,6 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2019-10-24 16:08:58.145719 -0700 PDT m=+0.562621923 +// 2019-11-05 09:40:38.659073 -0800 PST m=+0.675924642 // // This contains the most recently Markdown source for the Statping Wiki. package source