Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
example : add an example "send email with event data received on http"
Browse files Browse the repository at this point in the history
core : use webhook.listen flags to set codeWebHookServer host and port
  • Loading branch information
vjeantet committed Jul 25, 2017
1 parent d73dbec commit edb3b97
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Start(addr string) {
//Init Store
myStore = NewMemory(dataLocation)
//Init Webhooks
listenAndServeWebHook("127.0.0.1:9090")
listenAndServeWebHook(addr)
}

// StartPipeline load all agents form a configPipeline and returns pipeline's ID
Expand Down
20 changes: 14 additions & 6 deletions core/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"fmt"
"net/http"
"strings"

"golang.org/x/sync/syncmap"

Expand All @@ -17,23 +18,28 @@ type webHook struct {
}

var webHookMap = syncmap.Map{}
var webHookAddr = ""
var httpHookServerMux *http.ServeMux

func newWebHook(pipelineLabel, nameSpace string) *webHook {
return &webHook{pipelineLabel: pipelineLabel, namespace: nameSpace, Hooks: []string{}}
}

func (w *webHook) buildURL(hookName string) string {
return strings.ToLower("/" + slug.Make(w.pipelineLabel) + "/" + slug.Make(w.namespace) + "/" + slug.Make(hookName))
}

// Add a new route to a given http.HandlerFunc
func (w *webHook) Add(hookName string, hf http.HandlerFunc) {
hUrl := "/" + slug.Make(w.pipelineLabel) + "/" + slug.Make(w.namespace) + "/" + slug.Make(hookName)
hUrl := w.buildURL(hookName)
w.Hooks = append(w.Hooks, hookName)
webHookMap.Store(hUrl, hf)
Log().Infof("Hook %s => %s", hookName, hUrl)
Log().Infof("Hook added [%s/%s] => %s", w.pipelineLabel, w.namespace, "http://"+webHookAddr+hUrl)
}

// Delete a route
func (w *webHook) Delete(hookName string) {
hUrl := "/" + slug.Make(w.pipelineLabel) + "/" + slug.Make(w.namespace) + "/" + slug.Make(hookName)
hUrl := w.buildURL(hookName)
webHookMap.Delete(hUrl)
Log().Debugf("WebHook unregisted [%s]", hUrl)
}
Expand All @@ -46,11 +52,12 @@ func (w *webHook) Unregister() {
}

func routerHandler(w http.ResponseWriter, r *http.Request) {
if hfi, ok := webHookMap.Load(r.URL.Path); ok {
Log().Debugf("Webhook found for %s", r.URL.Path)
hUrl := strings.ToLower(r.URL.Path)
if hfi, ok := webHookMap.Load(hUrl); ok {
Log().Debugf("Webhook found for %s", hUrl)
hfi.(http.HandlerFunc)(w, r)
} else {
Log().Warnf("Webhook not found for %s", r.URL.Path)
Log().Warnf("Webhook not found for %s", hUrl)
w.WriteHeader(404)
fmt.Fprint(w, "Not Found !")
}
Expand All @@ -60,6 +67,7 @@ func listenAndServeWebHook(addr string) {
httpHookServerMux = http.NewServeMux()
commonHandlers := alice.New(loggingHandler, recoverHandler)
httpHookServerMux.Handle("/", commonHandlers.ThenFunc(routerHandler))
webHookAddr = addr
go http.ListenAndServe(addr, httpHookServerMux)

Log().Infof("Agents webHook listening on %s", addr)
Expand Down
43 changes: 43 additions & 0 deletions examples.d/sendEmailOnHTTP.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# send a email when a http request comes to http://127.0.0.1:19090/sendEmailOnHTTP/welcome/send

input{
# print link to stdout on pipeline start
stdout{
codec => line {
format => "go to http://127.0.0.1:19090/sendEmailOnHTTP/welcome/send?name=Jon"
delimiter => " : "
}
}

httpserver "welcome"{
codec=>plain
uri="send"
}
}

filter{
template {
target => "output"
location => "<h1>Hello {{index .request.querystring.name 0}} !</h1>"
}

# print event to stdout (debug)
stdout{
codec=> rubydebug
}
}


output{
email{
address => "bananapi.local"
port => 1025

from => "bitfan@nowhere.com"
to => "me@host.com, you@host.com"
cc => "mecc@host.com, youcc@host.com"

subject => 'Bitfan on {{TS "dd/MM/YYYY" .}}'
htmlbody => "{{.output | SafeHTML}}"
}
}

0 comments on commit edb3b97

Please sign in to comment.