From 2a7d0c19a583f18ecdd27f6dd2e928ba5434dc03 Mon Sep 17 00:00:00 2001 From: franklin Date: Tue, 3 Jan 2017 19:32:07 +0100 Subject: [PATCH] added updated webhooks --- neosproxy.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/neosproxy.go b/neosproxy.go index 6bd5454..85a016b 100644 --- a/neosproxy.go +++ b/neosproxy.go @@ -1,6 +1,9 @@ package main import ( + "bytes" + "crypto/tls" + "encoding/json" "errors" "flag" "fmt" @@ -11,6 +14,7 @@ import ( "net/http/httputil" "net/url" "os" + "strings" "time" ) @@ -111,15 +115,50 @@ func (p *Proxy) cacheNeosContentServerExport() (err error) { } log.Println(fmt.Sprintf("%d\t%s\t%s", http.StatusOK, "/contentserverproxy/cache", "got new contentserver export from neos")) + + // Notify webhooks + if len(p.UpdatedHooks) > 0 { + p.notify("updated", p.UpdatedHooks) + } + return nil } +func (p *Proxy) notify(event string, urls []string) { + log.Println(fmt.Sprintf("Notifying %d for '%s' event", len(urls), event)) + for _, url := range p.UpdatedHooks { + data, _ := json.Marshal(map[string]string{ + "type": event, + }) + go func() { + var client *http.Client + if p.TLSHooks { + client = &http.Client{} + } else { + client = &http.Client{Transport: &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: p.TLSHooksSkipVerify}, + }, + } + } + resp, err := client.Post(url, "application/json", bytes.NewBuffer(data)) + if err != nil { + log.Println(fmt.Sprintf("Failed to notify a webhook! Got error: %s", err.Error())) + } else { + log.Println(fmt.Sprintf("Notified webhook with response code: %d", resp.StatusCode)) + } + }() + } +} + type Proxy struct { APIKey string Address string Endpoint *url.URL FilenameCachedContentServerExport string CacheInvalidationChannel chan time.Time + UpdatedHooks []string + TLSHooks bool + TLSHooksSkipVerify bool } func (p Proxy) run() { @@ -154,6 +193,9 @@ func main() { flagAddress := flag.String("address", "0.0.0.0:80", "address to listen to") flagNeosHostname := flag.String("neos", "http://neos/", "neos cms hostname") + flagTLSHooks := flag.Bool("tls-hooks", false, "enable TLS on webhook notifications") + flagTLSHooksSkipVerify := flag.Bool("tls-hooks-skip-verify", false, "skip TLS verification on webhook notifications") + flagUpdatedHooks := flag.String("updated-hooks", "", "comma seperated list of urls to notify on update event") flag.Parse() neosURL, err := url.Parse(*flagNeosHostname) @@ -167,6 +209,9 @@ func main() { Endpoint: neosURL, CacheInvalidationChannel: make(chan time.Time, 1), FilenameCachedContentServerExport: os.TempDir() + "neos-contentserverexport.json", + UpdatedHooks: strings.Split(*flagUpdatedHooks, ","), + TLSHooks: *flagTLSHooks, + TLSHooksSkipVerify: *flagTLSHooksSkipVerify, } fmt.Println("start proxy on ", *flagAddress, "for neos", *flagNeosHostname, "with cache file in", p.FilenameCachedContentServerExport)