mirror of https://github.com/statping/statping
notifier update
parent
a4f9387de0
commit
1197b09b29
|
@ -18,7 +18,7 @@ services:
|
||||||
|
|
||||||
env:
|
env:
|
||||||
global:
|
global:
|
||||||
- VERSION=0.29.8
|
- VERSION=0.29.9
|
||||||
- DB_HOST=localhost
|
- DB_HOST=localhost
|
||||||
- DB_USER=travis
|
- DB_USER=travis
|
||||||
- DB_PASS=
|
- DB_PASS=
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
|
|
||||||
ENV VERSION=v0.29.8
|
ENV VERSION=v0.29.9
|
||||||
|
|
||||||
RUN apk --no-cache add libstdc++ ca-certificates
|
RUN apk --no-cache add libstdc++ ca-certificates
|
||||||
RUN wget -q https://github.com/hunterlong/statup/releases/download/$VERSION/statup-linux-alpine.tar.gz && \
|
RUN wget -q https://github.com/hunterlong/statup/releases/download/$VERSION/statup-linux-alpine.tar.gz && \
|
||||||
|
|
|
@ -37,7 +37,7 @@ func Router() *mux.Router {
|
||||||
r.Handle("/settings", http.HandlerFunc(SaveSettingsHandler)).Methods("POST")
|
r.Handle("/settings", http.HandlerFunc(SaveSettingsHandler)).Methods("POST")
|
||||||
r.Handle("/settings/css", http.HandlerFunc(SaveSASSHandler)).Methods("POST")
|
r.Handle("/settings/css", http.HandlerFunc(SaveSASSHandler)).Methods("POST")
|
||||||
r.Handle("/settings/build", http.HandlerFunc(SaveAssetsHandler)).Methods("GET")
|
r.Handle("/settings/build", http.HandlerFunc(SaveAssetsHandler)).Methods("GET")
|
||||||
r.Handle("/settings/notifier_{{id}}", http.HandlerFunc(SaveNotificationHandler)).Methods("POST")
|
r.Handle("/settings/notifier/{id}", http.HandlerFunc(SaveNotificationHandler)).Methods("POST")
|
||||||
r.Handle("/plugins/download/{name}", http.HandlerFunc(PluginsDownloadHandler))
|
r.Handle("/plugins/download/{name}", http.HandlerFunc(PluginsDownloadHandler))
|
||||||
r.Handle("/plugins/{name}/save", http.HandlerFunc(PluginSavedHandler)).Methods("POST")
|
r.Handle("/plugins/{name}/save", http.HandlerFunc(PluginSavedHandler)).Methods("POST")
|
||||||
r.Handle("/help", http.HandlerFunc(HelpHandler))
|
r.Handle("/help", http.HandlerFunc(HelpHandler))
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
"github.com/hunterlong/statup/core"
|
"github.com/hunterlong/statup/core"
|
||||||
"github.com/hunterlong/statup/notifiers"
|
"github.com/hunterlong/statup/notifiers"
|
||||||
"github.com/hunterlong/statup/utils"
|
"github.com/hunterlong/statup/utils"
|
||||||
|
@ -84,13 +86,15 @@ func SaveAssetsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func SaveNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
func SaveNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var err error
|
||||||
if !IsAuthenticated(r) {
|
if !IsAuthenticated(r) {
|
||||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
vars := mux.Vars(r)
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
|
|
||||||
notifierId := r.PostForm.Get("id")
|
notifierId := vars["id"]
|
||||||
enabled := r.PostForm.Get("enable")
|
enabled := r.PostForm.Get("enable")
|
||||||
|
|
||||||
host := r.PostForm.Get("host")
|
host := r.PostForm.Get("host")
|
||||||
|
@ -103,6 +107,7 @@ func SaveNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
apiSecret := r.PostForm.Get("api_secret")
|
apiSecret := r.PostForm.Get("api_secret")
|
||||||
limits := int64(utils.StringInt(r.PostForm.Get("limits")))
|
limits := int64(utils.StringInt(r.PostForm.Get("limits")))
|
||||||
notifer := notifiers.Select(utils.StringInt(notifierId))
|
notifer := notifiers.Select(utils.StringInt(notifierId))
|
||||||
|
|
||||||
if host != "" {
|
if host != "" {
|
||||||
notifer.Host = host
|
notifer.Host = host
|
||||||
}
|
}
|
||||||
|
@ -135,7 +140,7 @@ func SaveNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
} else {
|
} else {
|
||||||
notifer.Enabled = false
|
notifer.Enabled = false
|
||||||
}
|
}
|
||||||
notifer, err := notifer.Update()
|
notifer, err = notifer.Update()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Log(3, err)
|
utils.Log(3, err)
|
||||||
}
|
}
|
||||||
|
@ -145,5 +150,7 @@ func SaveNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
go notify.Run()
|
go notify.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
utils.Log(1, fmt.Sprintf("Notifier saved: %v", notifer))
|
||||||
|
|
||||||
http.Redirect(w, r, "/settings", http.StatusSeeOther)
|
http.Redirect(w, r, "/settings", http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,14 @@
|
||||||
package notifiers
|
package notifiers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/GeertJohan/go.rice"
|
||||||
|
"github.com/hunterlong/statup/types"
|
||||||
"github.com/hunterlong/statup/utils"
|
"github.com/hunterlong/statup/utils"
|
||||||
|
"gopkg.in/gomail.v2"
|
||||||
|
"html/template"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,56 +20,59 @@ const (
|
||||||
var (
|
var (
|
||||||
emailer *Email
|
emailer *Email
|
||||||
emailArray []string
|
emailArray []string
|
||||||
|
emailQueue []*types.Email
|
||||||
)
|
)
|
||||||
|
|
||||||
type Email struct {
|
type Email struct {
|
||||||
*Notification
|
*Notification
|
||||||
|
mailer *gomail.Dialer
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEFINE YOUR NOTIFICATION HERE.
|
// DEFINE YOUR NOTIFICATION HERE.
|
||||||
func init() {
|
func init() {
|
||||||
|
|
||||||
emailer = &Email{&Notification{
|
emailer = &Email{
|
||||||
Id: EMAIL_ID,
|
Notification: &Notification{
|
||||||
Method: EMAIL_METHOD,
|
Id: EMAIL_ID,
|
||||||
Form: []NotificationForm{{
|
Method: EMAIL_METHOD,
|
||||||
id: 1,
|
Form: []NotificationForm{{
|
||||||
Type: "text",
|
id: 1,
|
||||||
Title: "SMTP Host",
|
Type: "text",
|
||||||
Placeholder: "Insert your SMTP Host here.",
|
Title: "SMTP Host",
|
||||||
DbField: "Host",
|
Placeholder: "Insert your SMTP Host here.",
|
||||||
}, {
|
DbField: "Host",
|
||||||
id: 1,
|
}, {
|
||||||
Type: "text",
|
id: 1,
|
||||||
Title: "SMTP Username",
|
Type: "text",
|
||||||
Placeholder: "Insert your SMTP Username here.",
|
Title: "SMTP Username",
|
||||||
DbField: "Username",
|
Placeholder: "Insert your SMTP Username here.",
|
||||||
}, {
|
DbField: "Username",
|
||||||
id: 1,
|
}, {
|
||||||
Type: "password",
|
id: 1,
|
||||||
Title: "SMTP Password",
|
Type: "password",
|
||||||
Placeholder: "Insert your SMTP Password here.",
|
Title: "SMTP Password",
|
||||||
DbField: "Password",
|
Placeholder: "Insert your SMTP Password here.",
|
||||||
}, {
|
DbField: "Password",
|
||||||
id: 1,
|
}, {
|
||||||
Type: "number",
|
id: 1,
|
||||||
Title: "SMTP Port",
|
Type: "number",
|
||||||
Placeholder: "Insert your SMTP Port here.",
|
Title: "SMTP Port",
|
||||||
DbField: "Port",
|
Placeholder: "Insert your SMTP Port here.",
|
||||||
}, {
|
DbField: "Port",
|
||||||
id: 1,
|
}, {
|
||||||
Type: "text",
|
id: 1,
|
||||||
Title: "Outgoing Email Address",
|
Type: "text",
|
||||||
Placeholder: "Insert your Outgoing Email Address",
|
Title: "Outgoing Email Address",
|
||||||
DbField: "Var1",
|
Placeholder: "Insert your Outgoing Email Address",
|
||||||
}, {
|
DbField: "Var1",
|
||||||
id: 1,
|
}, {
|
||||||
Type: "number",
|
id: 1,
|
||||||
Title: "Limits per Hour",
|
Type: "number",
|
||||||
Placeholder: "How many emails can it send per hour",
|
Title: "Limits per Hour",
|
||||||
DbField: "Limits",
|
Placeholder: "How many emails can it send per hour",
|
||||||
}},
|
DbField: "Limits",
|
||||||
}}
|
}},
|
||||||
|
}}
|
||||||
|
|
||||||
add(emailer)
|
add(emailer)
|
||||||
}
|
}
|
||||||
|
@ -75,37 +84,62 @@ func (u *Email) Select() *Notification {
|
||||||
|
|
||||||
// WHEN NOTIFIER LOADS
|
// WHEN NOTIFIER LOADS
|
||||||
func (u *Email) Init() error {
|
func (u *Email) Init() error {
|
||||||
//err := SendSlack("its online")
|
err := u.Install()
|
||||||
|
|
||||||
u.Install()
|
if err == nil {
|
||||||
|
notifier, _ := SelectNotification(u.Id)
|
||||||
|
forms := u.Form
|
||||||
|
u.Notification = notifier
|
||||||
|
u.Form = forms
|
||||||
|
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}
|
||||||
|
|
||||||
|
go u.Run()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//go u.Run()
|
//go u.Run()
|
||||||
return nil
|
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)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// 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
|
||||||
//for _, email := range emailArray {
|
for _, email := range emailQueue {
|
||||||
// if inArray(sentAddresses, email.To) || email.Sent {
|
if inArray(sentAddresses, email.To) || email.Sent {
|
||||||
// emailQueue = removeEmail(emailQueue, email)
|
emailQueue = removeEmail(emailQueue, email)
|
||||||
// continue
|
continue
|
||||||
// }
|
}
|
||||||
// e := email
|
e := email
|
||||||
// go func(email *types.Email) {
|
go func(email *types.Email) {
|
||||||
// err := dialSend(email)
|
err := u.dialSend(email)
|
||||||
// if err == nil {
|
if err == nil {
|
||||||
// email.Sent = true
|
email.Sent = true
|
||||||
// sentAddresses = append(sentAddresses, email.To)
|
sentAddresses = append(sentAddresses, email.To)
|
||||||
// utils.Log(1, fmt.Sprintf("Email '%v' sent to: %v using the %v template (size: %v)", email.Subject, email.To, email.Template, len([]byte(email.Source))))
|
utils.Log(1, fmt.Sprintf("Email '%v' sent to: %v using the %v template (size: %v)", email.Subject, email.To, email.Template, len([]byte(email.Source))))
|
||||||
// emailQueue = removeEmail(emailQueue, email)
|
emailQueue = removeEmail(emailQueue, email)
|
||||||
// }
|
}
|
||||||
// }(e)
|
}(e)
|
||||||
//}
|
}
|
||||||
time.Sleep(60 * time.Second)
|
time.Sleep(60 * time.Second)
|
||||||
//if EmailComm.Enabled {
|
if u.Enabled {
|
||||||
//EmailRoutine()
|
return u.Run()
|
||||||
//}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,3 +183,59 @@ func (u *Email) Install() error {
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *Email) dialSend(email *types.Email) error {
|
||||||
|
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 {
|
||||||
|
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 nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func SendEmail(box *rice.Box, email *types.Email) {
|
||||||
|
source := EmailTemplate(box, email.Template, email.Data)
|
||||||
|
email.Source = source
|
||||||
|
emailQueue = append(emailQueue, email)
|
||||||
|
}
|
||||||
|
|
||||||
|
func EmailTemplate(box *rice.Box, tmpl string, data interface{}) string {
|
||||||
|
emailTpl, err := box.String(tmpl)
|
||||||
|
if err != nil {
|
||||||
|
utils.Log(3, err)
|
||||||
|
}
|
||||||
|
t := template.New("email")
|
||||||
|
t, err = t.Parse(emailTpl)
|
||||||
|
if err != nil {
|
||||||
|
utils.Log(3, err)
|
||||||
|
}
|
||||||
|
var tpl bytes.Buffer
|
||||||
|
if err := t.Execute(&tpl, data); err != nil {
|
||||||
|
utils.Log(2, err)
|
||||||
|
}
|
||||||
|
result := tpl.String()
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeEmail(emails []*types.Email, em *types.Email) []*types.Email {
|
||||||
|
var newArr []*types.Email
|
||||||
|
for _, e := range emails {
|
||||||
|
if e != em {
|
||||||
|
newArr = append(newArr, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newArr
|
||||||
|
}
|
||||||
|
|
||||||
|
func inArray(a []string, v string) bool {
|
||||||
|
for _, i := range a {
|
||||||
|
if i == v {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@ func Load() []AllNotifiers {
|
||||||
n := comm.(Notifier)
|
n := comm.(Notifier)
|
||||||
n.Init()
|
n.Init()
|
||||||
notifiers = append(notifiers, n)
|
notifiers = append(notifiers, n)
|
||||||
|
n.Test()
|
||||||
}
|
}
|
||||||
return notifiers
|
return notifiers
|
||||||
}
|
}
|
||||||
|
@ -56,6 +57,7 @@ type Notifier interface {
|
||||||
OnFailure() error
|
OnFailure() error
|
||||||
OnSuccess() error
|
OnSuccess() error
|
||||||
Select() *Notification
|
Select() *Notification
|
||||||
|
Test() error
|
||||||
}
|
}
|
||||||
|
|
||||||
type NotificationForm struct {
|
type NotificationForm struct {
|
||||||
|
@ -79,6 +81,7 @@ func SelectNotification(id int64) (*Notification, error) {
|
||||||
func (n *Notification) Update() (*Notification, error) {
|
func (n *Notification) Update() (*Notification, error) {
|
||||||
n.CreatedAt = time.Now()
|
n.CreatedAt = time.Now()
|
||||||
err := Collections.Find("id", n.Id).Update(n)
|
err := Collections.Find("id", n.Id).Update(n)
|
||||||
|
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,7 +110,10 @@ func SelectNotifier(id int64) Notifier {
|
||||||
var notifier Notifier
|
var notifier Notifier
|
||||||
for _, n := range AllCommunications {
|
for _, n := range AllCommunications {
|
||||||
notif := n.(Notifier)
|
notif := n.(Notifier)
|
||||||
return notif
|
n := notif.Select()
|
||||||
|
if n.Id == id {
|
||||||
|
return notif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return notifier
|
return notifier
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,6 +62,11 @@ func (u *Slack) Init() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *Slack) Test() error {
|
||||||
|
SendSlack("Slack notifications on your Statup server is working!")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// AFTER NOTIFIER LOADS, IF ENABLED, START A QUEUE PROCESS
|
// AFTER NOTIFIER LOADS, IF ENABLED, START A QUEUE PROCESS
|
||||||
func (u *Slack) Run() error {
|
func (u *Slack) Run() error {
|
||||||
for _, msg := range slackMessages {
|
for _, msg := range slackMessages {
|
||||||
|
|
|
@ -123,7 +123,7 @@
|
||||||
|
|
||||||
{{ range .Communications }}
|
{{ range .Communications }}
|
||||||
<div class="tab-pane fade" id="v-pills-{{underscore .Method}}" role="tabpanel" aria-labelledby="v-pills-{{underscore .Method }}-tab">
|
<div class="tab-pane fade" id="v-pills-{{underscore .Method}}" role="tabpanel" aria-labelledby="v-pills-{{underscore .Method }}-tab">
|
||||||
<form method="POST" action="/settings/notifier_{{ .Id }}">
|
<form method="POST" action="/settings/notifier/{{ .Id }}">
|
||||||
{{range .Form}}
|
{{range .Form}}
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label class="text-capitalize" for="{{underscore .Title}}">{{.Title}}</label>
|
<label class="text-capitalize" for="{{underscore .Title}}">{{.Title}}</label>
|
||||||
|
|
Loading…
Reference in New Issue