feat(alerting): skeleton commit for webhook

This commit is contained in:
bergquist
2016-06-15 14:45:05 +02:00
parent a3b7ea7704
commit efea3bc9cb
5 changed files with 107 additions and 4 deletions

View File

@@ -23,11 +23,14 @@ var tmplWelcomeOnSignUp = "welcome_on_signup.html"
func Init() error {
initMailQueue()
initWebhookQueue()
bus.AddHandler("email", sendResetPasswordEmail)
bus.AddHandler("email", validateResetPasswordCode)
bus.AddHandler("email", sendEmailCommandHandler)
bus.AddHandler("webhook", sendWebhook)
bus.AddEventListener(signUpStartedHandler)
bus.AddEventListener(signUpCompletedHandler)
@@ -53,6 +56,18 @@ func Init() error {
return nil
}
func sendWebhook(cmd *m.SendWebhook) error {
addToWebhookQueue(&Webhook{
Url: cmd.Url,
AuthUser: cmd.AuthUser,
AuthPassword: cmd.AuthPassword,
Method: cmd.Method,
Body: cmd.Body,
})
return nil
}
func subjectTemplateFunc(obj map[string]interface{}, value string) string {
obj["value"] = value
return ""

View File

@@ -0,0 +1,62 @@
package notifications
import (
"net/http"
"time"
"github.com/grafana/grafana/pkg/log"
)
type Webhook struct {
Url string
AuthUser string
AuthPassword string
Body string
Method string
}
var webhookQueue chan *Webhook
var webhookLog log.Logger
func initWebhookQueue() {
webhookLog = log.New("notifications.webhook")
webhookQueue = make(chan *Webhook, 10)
go processWebhookQueue()
}
func processWebhookQueue() {
for {
select {
case webhook := <-webhookQueue:
err := sendWebRequest(webhook)
if err != nil {
webhookLog.Error("Failed to send webrequest ")
}
}
}
}
func sendWebRequest(webhook *Webhook) error {
webhookLog.Error("Sending stuff! ", "url", webhook.Url)
client := http.Client{Timeout: time.Duration(3 * time.Second)}
request, err := http.NewRequest(webhook.Method, webhook.Url, nil /*io.reader*/)
if err != nil {
return err
}
resp, err := client.Do(request)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
var addToWebhookQueue = func(msg *Webhook) {
webhookQueue <- msg
}