From 949ba3958aa427d2145d10cdcc5acb83ab3d1747 Mon Sep 17 00:00:00 2001 From: Hunter Long Date: Mon, 16 Jul 2018 00:02:33 -0700 Subject: [PATCH] notifiers - split interfaces/packages --- .gitignore | 2 +- .travis.yml | 2 +- core/assets.go | 4 +- core/core.go | 1 - main.go | 1 - notifiers/email.go | 58 ++++++++++++++++------- {source => notifiers}/emails/failure.html | 6 +-- {source => notifiers}/emails/message.html | 0 notifiers/notifiers.go | 2 +- notifiers/rice-box.go | 48 +++++++++++++++++++ notifiers/slack.go | 17 ++++--- 11 files changed, 103 insertions(+), 38 deletions(-) rename {source => notifiers}/emails/failure.html (84%) rename {source => notifiers}/emails/message.html (100%) create mode 100644 notifiers/rice-box.go diff --git a/.gitignore b/.gitignore index da7e1961..68fa2d42 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ .idea -rice-box.go +./rice-box.go config.yml statup.db plugins/*.so diff --git a/.travis.yml b/.travis.yml index fcc62ea1..2bba6816 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,7 +18,7 @@ services: env: global: - - VERSION=0.29.9 + - VERSION=0.30 - DB_HOST=localhost - DB_USER=travis - DB_PASS= diff --git a/core/assets.go b/core/assets.go index fa7d1c99..fbaa6597 100644 --- a/core/assets.go +++ b/core/assets.go @@ -95,8 +95,8 @@ func CreateAllAssets() { utils.Log(1, "Inserting scss, css, emails, and javascript files into assets..") CopyToPublic(ScssBox, "scss", "base.scss") CopyToPublic(ScssBox, "scss", "variables.scss") - CopyToPublic(EmailBox, "emails", "message.html") - CopyToPublic(EmailBox, "emails", "failure.html") + //CopyToPublic(EmailBox, "emails", "message.html") + //CopyToPublic(EmailBox, "emails", "failure.html") CopyToPublic(CssBox, "css", "bootstrap.min.css") CopyToPublic(JsBox, "js", "bootstrap.min.js") CopyToPublic(JsBox, "js", "Chart.bundle.min.js") diff --git a/core/core.go b/core/core.go index 1154147a..7c75c969 100644 --- a/core/core.go +++ b/core/core.go @@ -42,7 +42,6 @@ var ( ScssBox *rice.Box JsBox *rice.Box TmplBox *rice.Box - EmailBox *rice.Box SetupMode bool UsingAssets bool VERSION string diff --git a/main.go b/main.go index cdc76b76..99ba6d33 100644 --- a/main.go +++ b/main.go @@ -50,7 +50,6 @@ func RenderBoxes() { core.ScssBox = rice.MustFindBox("source/scss") core.JsBox = rice.MustFindBox("source/js") core.TmplBox = rice.MustFindBox("source/tmpl") - core.EmailBox = rice.MustFindBox("source/emails") } func LoadDotEnvs() { diff --git a/notifiers/email.go b/notifiers/email.go index 35e5fbd8..e9f14bb9 100644 --- a/notifiers/email.go +++ b/notifiers/email.go @@ -21,11 +21,12 @@ var ( emailer *Email emailArray []string emailQueue []*types.Email + emailBox *rice.Box + mailer *gomail.Dialer ) type Email struct { *Notification - mailer *gomail.Dialer } // DEFINE YOUR NOTIFICATION HERE. @@ -84,7 +85,9 @@ func (u *Email) Select() *Notification { // WHEN NOTIFIER LOADS func (u *Email) Init() error { + emailBox = rice.MustFindBox("emails") err := u.Install() + utils.Log(1, fmt.Sprintf("Creating Mailer: %v:%v", u.Notification.Host, u.Notification.Port)) if err == nil { notifier, _ := SelectNotification(u.Id) @@ -94,29 +97,33 @@ func (u *Email) Init() error { if u.Enabled { utils.Log(1, fmt.Sprintf("Loading SMTP Emailer using host: %v:%v", u.Notification.Host, u.Notification.Port)) - u.mailer = gomail.NewDialer(u.Notification.Host, u.Notification.Port, u.Notification.Username, u.Notification.Password) - u.mailer.TLSConfig = &tls.Config{InsecureSkipVerify: true} + mailer = gomail.NewDialer(u.Notification.Host, u.Notification.Port, u.Notification.Username, u.Notification.Password) + mailer.TLSConfig = &tls.Config{InsecureSkipVerify: true} go u.Run() } } - - //go u.Run() return nil } func (u *Email) Test() error { - //email := &types.Email{ - // To: "info@socialeck.com", - // Subject: "Test Email", - // Template: "message.html", - // Data: nil, - // From: emailer.Var1, - //} - //SendEmail(core.EmailBox, email) + if u.Enabled { + email := &types.Email{ + To: "info@socialeck.com", + Subject: "Test Email", + Template: "message.html", + Data: nil, + From: emailer.Var1, + } + SendEmail(emailBox, email) + } return nil } +type emailMessage struct { + Service *types.Service +} + // AFTER NOTIFIER LOADS, IF ENABLED, START A QUEUE PROCESS func (u *Email) Run() error { var sentAddresses []string @@ -146,8 +153,20 @@ func (u *Email) Run() error { // ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS func (u *Email) OnFailure(s *types.Service) error { if u.Enabled { - utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method)) - // Do failing stuff here! + + msg := emailMessage{ + Service: s, + } + + email := &types.Email{ + To: "info@socialeck.com", + Subject: fmt.Sprintf("Service %v is Failing", s.Name), + Template: "failure.html", + Data: msg, + From: emailer.Var1, + } + SendEmail(emailBox, email) + } return nil } @@ -155,8 +174,7 @@ func (u *Email) OnFailure(s *types.Service) error { // ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS func (u *Email) OnSuccess(s *types.Service) error { if u.Enabled { - utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method)) - // Do failing stuff here! + } return nil } @@ -172,6 +190,9 @@ func (u *Email) OnSave() error { // ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS func (u *Email) Install() error { + + fmt.Println("installing emailer") + inDb, err := emailer.Notification.isInDatabase() if !inDb { newNotifer, err := InsertDatabase(u.Notification) @@ -185,12 +206,13 @@ func (u *Email) Install() error { } func (u *Email) dialSend(email *types.Email) error { + fmt.Println("sending dailsend to emailer") m := gomail.NewMessage() m.SetHeader("From", email.From) m.SetHeader("To", email.To) m.SetHeader("Subject", email.Subject) m.SetBody("text/html", email.Source) - if err := u.mailer.DialAndSend(m); err != nil { + if err := mailer.DialAndSend(m); err != nil { utils.Log(3, fmt.Sprintf("Email '%v' sent to: %v using the %v template (size: %v) %v", email.Subject, email.To, email.Template, len([]byte(email.Source)), err)) return err } diff --git a/source/emails/failure.html b/notifiers/emails/failure.html similarity index 84% rename from source/emails/failure.html rename to notifiers/emails/failure.html index ae964e3c..2d11e1c4 100644 --- a/source/emails/failure.html +++ b/notifiers/emails/failure.html @@ -50,15 +50,13 @@
- View Service + View Service - Statup Dashboard + Statup Dashboard
- - diff --git a/source/emails/message.html b/notifiers/emails/message.html similarity index 100% rename from source/emails/message.html rename to notifiers/emails/message.html diff --git a/notifiers/notifiers.go b/notifiers/notifiers.go index dfdaa242..a54f1786 100644 --- a/notifiers/notifiers.go +++ b/notifiers/notifiers.go @@ -169,7 +169,7 @@ func uniqueStrings(elements []string) []string { // Scan slice for a previous element of the same value. exists := false for v := 0; v < i; v++ { - if elements[v][:10] == elements[i][:10] { + if elements[v] == elements[i] { exists = true break } diff --git a/notifiers/rice-box.go b/notifiers/rice-box.go new file mode 100644 index 00000000..fc32458a --- /dev/null +++ b/notifiers/rice-box.go @@ -0,0 +1,48 @@ +package notifiers + +import ( + "github.com/GeertJohan/go.rice/embedded" + "time" +) + +func init() { + + // define files + file2 := &embedded.EmbeddedFile{ + Filename: "failure.html", + FileModTime: time.Unix(1531720141, 0), + Content: string("\n\n\n \n \n Sample Email\n\n\n\n\n\n \n \n \n
\n \n\n \n \n \n
\n \n\n \n \n \n
\n

{{ .Service.Name }} is Offline!

\n

\n Your Statup service '{{.Service.Name}}' has been triggered with a HTTP status code of '{{.Service.LastStatusCode}}' and is currently offline based on your requirements. This failure was created on {{.Service.CreatedAt}}.\n

\n\n {{if .Service.LastResponse }}\n

Last Response

\n

\n {{ .Service.LastResponse }}\n

\n {{end}}\n\n \n \n \n
\n View Service\n \n Statup Dashboard\n
\n
\n
\n
\n\n"), + } + file3 := &embedded.EmbeddedFile{ + Filename: "message.html", + FileModTime: time.Unix(1530546686, 0), + Content: string("\n\n\n \n \n Sample Email\n\n\n\n\n\n \n \n \n
\n \n\n \n \n \n
\n \n\n \n \n \n
\n

Looks Like Emails Work!

\n

\n Since you got this email, it confirms that your Statup Status Page email system is working correctly.\n

\n

\n

\n Enjoy using Statup!\n
Statup.io Team

\n\n \n\n
\n
\n
\n
\n\n"), + } + + // define dirs + dir1 := &embedded.EmbeddedDir{ + Filename: "", + DirModTime: time.Unix(1531720141, 0), + ChildFiles: []*embedded.EmbeddedFile{ + file2, // "failure.html" + file3, // "message.html" + + }, + } + + // link ChildDirs + dir1.ChildDirs = []*embedded.EmbeddedDir{} + + // register embeddedBox + embedded.RegisterEmbeddedBox(`emails`, &embedded.EmbeddedBox{ + Name: `emails`, + Time: time.Unix(1531720141, 0), + Dirs: map[string]*embedded.EmbeddedDir{ + "": dir1, + }, + Files: map[string]*embedded.EmbeddedFile{ + "failure.html": file2, + "message.html": file3, + }, + }) +} diff --git a/notifiers/slack.go b/notifiers/slack.go index 36328ca9..292e4b64 100644 --- a/notifiers/slack.go +++ b/notifiers/slack.go @@ -14,8 +14,8 @@ import ( const ( SLACK_ID = 2 SLACK_METHOD = "slack" - FAILING_TEMPLATE = `{ "attachments": [ { "fallback": "Service {{.Service.Name}} - is currently failing", "text": "<{{.Service.Domain}}|{{.Service.Name}}> - Your Statup service '{{.Service.Name}}' has just received a Failure notification with a HTTP Status code of {{.Service.LastStatusCode}}.", "fields": [ { "title": "Expected", "value": "{{.Service.Expected}}", "short": true }, { "title": "Status Code", "value": "{{.Service.LastStatusCode}}", "short": true } ], "color": "#FF0000", "thumb_url": "https://statup.io", "footer": "Statup", "footer_icon": "https://img.cjx.io/statuplogo32.png", "ts": {{.Time}} } ] }` - SUCCESS_TEMPLATE = `{ "attachments": [ { "fallback": "Service {{.Service.Name}} - is now back online", "text": "<{{.Service.Domain}}|{{.Service.Name}}> - Your Statup service '{{.Service.Name}}' has just received a Failure notification.", "fields": [ { "title": "Issue", "value": "Awesome Project", "short": true }, { "title": "Status Code", "value": "{{.Service.LastStatusCode}}", "short": true } ], "color": "#00FF00", "thumb_url": "https://statup.io", "footer": "Statup", "footer_icon": "https://img.cjx.io/statuplogo32.png", "ts": {{.Time}} } ] }` + FAILING_TEMPLATE = `{ "attachments": [ { "fallback": "Service {{.Service.Name}} - is currently failing", "text": "<{{.Service.Domain}}|{{.Service.Name}}> - Your Statup service '{{.Service.Name}}' has just received a Failure notification with a HTTP Status code of {{.Service.LastStatusCode}}.", "fields": [ { "title": "Expected", "value": "{{.Service.Expected}}", "short": true }, { "title": "Status Code", "value": "{{.Service.LastStatusCode}}", "short": true } ], "color": "#FF0000", "thumb_url": "https://statup.io", "footer": "Statup", "footer_icon": "https://img.cjx.io/statuplogo32.png" } ] }` + SUCCESS_TEMPLATE = `{ "attachments": [ { "fallback": "Service {{.Service.Name}} - is now back online", "text": "<{{.Service.Domain}}|{{.Service.Name}}> - Your Statup service '{{.Service.Name}}' has just received a Failure notification.", "fields": [ { "title": "Issue", "value": "Awesome Project", "short": true }, { "title": "Status Code", "value": "{{.Service.LastStatusCode}}", "short": true } ], "color": "#00FF00", "thumb_url": "https://statup.io", "footer": "Statup", "footer_icon": "https://img.cjx.io/statuplogo32.png" } ] }` TEST_TEMPLATE = `{"text":"{{.}}"}` ) @@ -90,9 +90,8 @@ func (u *Slack) Run() error { if err != nil { utils.Log(3, fmt.Sprintf("Issue sending Slack notification: %v", err)) } - - fmt.Println(msg) } + slackMessages = []string{} messageLock.Unlock() time.Sleep(60 * time.Second) if u.Enabled { @@ -115,13 +114,10 @@ func SendSlack(temp string, data interface{}) error { // ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS func (u *Slack) OnFailure(s *types.Service) error { if u.Enabled { - // Do failing stuff here! - message := slackMessage{ Service: s, Time: time.Now().Unix(), } - SendSlack(FAILING_TEMPLATE, message) } return nil @@ -130,8 +126,11 @@ func (u *Slack) OnFailure(s *types.Service) error { // ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS func (u *Slack) OnSuccess(s *types.Service) error { if u.Enabled { - SendSlack(SUCCESS_TEMPLATE, s) - // Do checking or any successful things here + //message := slackMessage{ + // Service: s, + // Time: time.Now().Unix(), + //} + //SendSlack(SUCCESS_TEMPLATE, message) } return nil }