notifiers - split interfaces/packages

pull/29/head
Hunter Long 2018-07-16 00:02:33 -07:00
parent 4ce7ca7530
commit 949ba3958a
11 changed files with 103 additions and 38 deletions

2
.gitignore vendored
View File

@ -1,5 +1,5 @@
.idea .idea
rice-box.go ./rice-box.go
config.yml config.yml
statup.db statup.db
plugins/*.so plugins/*.so

View File

@ -18,7 +18,7 @@ services:
env: env:
global: global:
- VERSION=0.29.9 - VERSION=0.30
- DB_HOST=localhost - DB_HOST=localhost
- DB_USER=travis - DB_USER=travis
- DB_PASS= - DB_PASS=

View File

@ -95,8 +95,8 @@ func CreateAllAssets() {
utils.Log(1, "Inserting scss, css, emails, and javascript files into assets..") utils.Log(1, "Inserting scss, css, emails, and javascript files into assets..")
CopyToPublic(ScssBox, "scss", "base.scss") CopyToPublic(ScssBox, "scss", "base.scss")
CopyToPublic(ScssBox, "scss", "variables.scss") CopyToPublic(ScssBox, "scss", "variables.scss")
CopyToPublic(EmailBox, "emails", "message.html") //CopyToPublic(EmailBox, "emails", "message.html")
CopyToPublic(EmailBox, "emails", "failure.html") //CopyToPublic(EmailBox, "emails", "failure.html")
CopyToPublic(CssBox, "css", "bootstrap.min.css") CopyToPublic(CssBox, "css", "bootstrap.min.css")
CopyToPublic(JsBox, "js", "bootstrap.min.js") CopyToPublic(JsBox, "js", "bootstrap.min.js")
CopyToPublic(JsBox, "js", "Chart.bundle.min.js") CopyToPublic(JsBox, "js", "Chart.bundle.min.js")

View File

@ -42,7 +42,6 @@ var (
ScssBox *rice.Box ScssBox *rice.Box
JsBox *rice.Box JsBox *rice.Box
TmplBox *rice.Box TmplBox *rice.Box
EmailBox *rice.Box
SetupMode bool SetupMode bool
UsingAssets bool UsingAssets bool
VERSION string VERSION string

View File

@ -50,7 +50,6 @@ func RenderBoxes() {
core.ScssBox = rice.MustFindBox("source/scss") core.ScssBox = rice.MustFindBox("source/scss")
core.JsBox = rice.MustFindBox("source/js") core.JsBox = rice.MustFindBox("source/js")
core.TmplBox = rice.MustFindBox("source/tmpl") core.TmplBox = rice.MustFindBox("source/tmpl")
core.EmailBox = rice.MustFindBox("source/emails")
} }
func LoadDotEnvs() { func LoadDotEnvs() {

View File

@ -21,11 +21,12 @@ var (
emailer *Email emailer *Email
emailArray []string emailArray []string
emailQueue []*types.Email emailQueue []*types.Email
emailBox *rice.Box
mailer *gomail.Dialer
) )
type Email struct { type Email struct {
*Notification *Notification
mailer *gomail.Dialer
} }
// DEFINE YOUR NOTIFICATION HERE. // DEFINE YOUR NOTIFICATION HERE.
@ -84,7 +85,9 @@ func (u *Email) Select() *Notification {
// WHEN NOTIFIER LOADS // WHEN NOTIFIER LOADS
func (u *Email) Init() error { func (u *Email) Init() error {
emailBox = rice.MustFindBox("emails")
err := u.Install() err := u.Install()
utils.Log(1, fmt.Sprintf("Creating Mailer: %v:%v", u.Notification.Host, u.Notification.Port))
if err == nil { if err == nil {
notifier, _ := SelectNotification(u.Id) notifier, _ := SelectNotification(u.Id)
@ -94,29 +97,33 @@ func (u *Email) Init() error {
if u.Enabled { if u.Enabled {
utils.Log(1, fmt.Sprintf("Loading SMTP Emailer using host: %v:%v", u.Notification.Host, u.Notification.Port)) 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) mailer = gomail.NewDialer(u.Notification.Host, u.Notification.Port, u.Notification.Username, u.Notification.Password)
u.mailer.TLSConfig = &tls.Config{InsecureSkipVerify: true} mailer.TLSConfig = &tls.Config{InsecureSkipVerify: true}
go u.Run() go u.Run()
} }
} }
//go u.Run()
return nil return nil
} }
func (u *Email) Test() error { func (u *Email) Test() error {
//email := &types.Email{ if u.Enabled {
// To: "info@socialeck.com", email := &types.Email{
// Subject: "Test Email", To: "info@socialeck.com",
// Template: "message.html", Subject: "Test Email",
// Data: nil, Template: "message.html",
// From: emailer.Var1, Data: nil,
//} From: emailer.Var1,
//SendEmail(core.EmailBox, email) }
SendEmail(emailBox, email)
}
return nil return nil
} }
type emailMessage struct {
Service *types.Service
}
// AFTER NOTIFIER LOADS, IF ENABLED, START A QUEUE PROCESS // AFTER NOTIFIER LOADS, IF ENABLED, START A QUEUE PROCESS
func (u *Email) Run() error { func (u *Email) Run() error {
var sentAddresses []string var sentAddresses []string
@ -146,8 +153,20 @@ func (u *Email) Run() error {
// ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS // ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS
func (u *Email) OnFailure(s *types.Service) error { func (u *Email) OnFailure(s *types.Service) error {
if u.Enabled { 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 return nil
} }
@ -155,8 +174,7 @@ func (u *Email) OnFailure(s *types.Service) error {
// ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS // ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS
func (u *Email) OnSuccess(s *types.Service) error { func (u *Email) OnSuccess(s *types.Service) error {
if u.Enabled { if u.Enabled {
utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method))
// Do failing stuff here!
} }
return nil return nil
} }
@ -172,6 +190,9 @@ func (u *Email) OnSave() error {
// ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS // ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS
func (u *Email) Install() error { func (u *Email) Install() error {
fmt.Println("installing emailer")
inDb, err := emailer.Notification.isInDatabase() inDb, err := emailer.Notification.isInDatabase()
if !inDb { if !inDb {
newNotifer, err := InsertDatabase(u.Notification) newNotifer, err := InsertDatabase(u.Notification)
@ -185,12 +206,13 @@ func (u *Email) Install() error {
} }
func (u *Email) dialSend(email *types.Email) error { func (u *Email) dialSend(email *types.Email) error {
fmt.Println("sending dailsend to emailer")
m := gomail.NewMessage() m := gomail.NewMessage()
m.SetHeader("From", email.From) m.SetHeader("From", email.From)
m.SetHeader("To", email.To) m.SetHeader("To", email.To)
m.SetHeader("Subject", email.Subject) m.SetHeader("Subject", email.Subject)
m.SetBody("text/html", email.Source) 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)) 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 return err
} }

View File

@ -50,15 +50,13 @@
<table class="body-sub" style="border-top-color: #EDEFF2; border-top-style: solid; border-top-width: 1px; box-sizing: border-box; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; margin-top: 25px; padding-top: 25px;"> <table class="body-sub" style="border-top-color: #EDEFF2; border-top-style: solid; border-top-width: 1px; box-sizing: border-box; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; margin-top: 25px; padding-top: 25px;">
<td style="box-sizing: border-box; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; word-break: break-word;"> <td style="box-sizing: border-box; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; word-break: break-word;">
<a href="{{.Domain}}/service/{{.Service.Id}}" class="button button--blue" target="_blank" style="-webkit-text-size-adjust: none; background: #3869D4; border-color: #3869d4; border-radius: 3px; border-style: solid; border-width: 10px 18px; box-shadow: 0 2px 3px rgba(0, 0, 0, 0.16); box-sizing: border-box; color: #FFF; display: inline-block; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; text-decoration: none;">View Service</a> <a href="/service/{{.Service.Id}}" class="button button--blue" target="_blank" style="-webkit-text-size-adjust: none; background: #3869D4; border-color: #3869d4; border-radius: 3px; border-style: solid; border-width: 10px 18px; box-shadow: 0 2px 3px rgba(0, 0, 0, 0.16); box-sizing: border-box; color: #FFF; display: inline-block; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; text-decoration: none;">View Service</a>
</td> </td>
<td style="box-sizing: border-box; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; word-break: break-word;"> <td style="box-sizing: border-box; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; word-break: break-word;">
<a href="{{.Domain}}/dashboard" class="button button--blue" target="_blank" style="-webkit-text-size-adjust: none; background: #3869D4; border-color: #3869d4; border-radius: 3px; border-style: solid; border-width: 10px 18px; box-shadow: 0 2px 3px rgba(0, 0, 0, 0.16); box-sizing: border-box; color: #FFF; display: inline-block; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; text-decoration: none;">Statup Dashboard</a> <a href="/dashboard" class="button button--blue" target="_blank" style="-webkit-text-size-adjust: none; background: #3869D4; border-color: #3869d4; border-radius: 3px; border-style: solid; border-width: 10px 18px; box-shadow: 0 2px 3px rgba(0, 0, 0, 0.16); box-sizing: border-box; color: #FFF; display: inline-block; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; text-decoration: none;">Statup Dashboard</a>
</td> </td>
</table> </table>
</td> </td>
</tr> </tr>
</table> </table>
</td> </td>

View File

@ -169,7 +169,7 @@ func uniqueStrings(elements []string) []string {
// Scan slice for a previous element of the same value. // Scan slice for a previous element of the same value.
exists := false exists := false
for v := 0; v < i; v++ { for v := 0; v < i; v++ {
if elements[v][:10] == elements[i][:10] { if elements[v] == elements[i] {
exists = true exists = true
break break
} }

48
notifiers/rice-box.go Normal file

File diff suppressed because one or more lines are too long

View File

@ -14,8 +14,8 @@ import (
const ( const (
SLACK_ID = 2 SLACK_ID = 2
SLACK_METHOD = "slack" 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}} } ] }` 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", "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" } ] }`
TEST_TEMPLATE = `{"text":"{{.}}"}` TEST_TEMPLATE = `{"text":"{{.}}"}`
) )
@ -90,9 +90,8 @@ func (u *Slack) Run() error {
if err != nil { if err != nil {
utils.Log(3, fmt.Sprintf("Issue sending Slack notification: %v", err)) utils.Log(3, fmt.Sprintf("Issue sending Slack notification: %v", err))
} }
fmt.Println(msg)
} }
slackMessages = []string{}
messageLock.Unlock() messageLock.Unlock()
time.Sleep(60 * time.Second) time.Sleep(60 * time.Second)
if u.Enabled { if u.Enabled {
@ -115,13 +114,10 @@ func SendSlack(temp string, data interface{}) error {
// ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS // ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS
func (u *Slack) OnFailure(s *types.Service) error { func (u *Slack) OnFailure(s *types.Service) error {
if u.Enabled { if u.Enabled {
// Do failing stuff here!
message := slackMessage{ message := slackMessage{
Service: s, Service: s,
Time: time.Now().Unix(), Time: time.Now().Unix(),
} }
SendSlack(FAILING_TEMPLATE, message) SendSlack(FAILING_TEMPLATE, message)
} }
return nil return nil
@ -130,8 +126,11 @@ func (u *Slack) OnFailure(s *types.Service) error {
// ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS // ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS
func (u *Slack) OnSuccess(s *types.Service) error { func (u *Slack) OnSuccess(s *types.Service) error {
if u.Enabled { if u.Enabled {
SendSlack(SUCCESS_TEMPLATE, s) //message := slackMessage{
// Do checking or any successful things here // Service: s,
// Time: time.Now().Unix(),
//}
//SendSlack(SUCCESS_TEMPLATE, message)
} }
return nil return nil
} }