This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core : webhook - delete processor's routes on processor stop, handle …
…panic
- Loading branch information
Showing
20 changed files
with
1,722 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,89 @@ | ||
package core | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"golang.org/x/sync/syncmap" | ||
|
||
"github.com/gosimple/slug" | ||
"github.com/justinas/alice" | ||
) | ||
|
||
var webHookMap = map[string]*webHook{} | ||
|
||
type webHook struct { | ||
mux *http.ServeMux | ||
namespace string | ||
uri string | ||
Hooks map[string]*hook | ||
pipelineLabel string | ||
namespace string | ||
Hooks []string | ||
} | ||
|
||
type hook struct { | ||
Url string | ||
handler *func(http.ResponseWriter, *http.Request) | ||
var webHookMap = syncmap.Map{} | ||
var httpHookServerMux *http.ServeMux | ||
|
||
func newWebHook(pipelineLabel, nameSpace string) *webHook { | ||
return &webHook{pipelineLabel: pipelineLabel, namespace: nameSpace, Hooks: []string{}} | ||
} | ||
|
||
func newWebHook(nameSpace string) *webHook { | ||
return &webHook{namespace: nameSpace, mux: httpHookServerMux, Hooks: map[string]*hook{}} | ||
// 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) | ||
w.Hooks = append(w.Hooks, hookName) | ||
webHookMap.Store(hUrl, hf) | ||
Log().Infof("Hook %s => %s", hookName, hUrl) | ||
} | ||
|
||
// Add register a new route matcher linked to hh | ||
func (w *webHook) Add(hookName string, hh func(http.ResponseWriter, *http.Request)) { | ||
h := &hook{} | ||
h.Url = slug.Make(w.namespace) + "/" + slug.Make(hookName) | ||
h.handler = &hh | ||
w.Hooks[hookName] = h | ||
w.mux.HandleFunc("/"+h.Url, *h.handler) | ||
Log().Infof("Hook %s => %s", hookName, "/"+h.Url) | ||
// Delete a route | ||
func (w *webHook) Delete(hookName string) { | ||
hUrl := "/" + slug.Make(w.pipelineLabel) + "/" + slug.Make(w.namespace) + "/" + slug.Make(hookName) | ||
webHookMap.Delete(hUrl) | ||
Log().Debugf("WebHook unregisted [%s]", hUrl) | ||
} | ||
|
||
func getAgentHooks(agentName string) (hooks map[string]*hook) { | ||
if _, ok := webHookMap[agentName]; ok { | ||
hooks = webHookMap[agentName].Hooks | ||
// Delete all routes belonging to webHook | ||
func (w *webHook) Unregister() { | ||
for _, hookName := range w.Hooks { | ||
w.Delete(hookName) | ||
} | ||
return hooks | ||
} | ||
|
||
var httpHookServerMux *http.ServeMux | ||
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) | ||
hfi.(http.HandlerFunc)(w, r) | ||
} else { | ||
Log().Warnf("Webhook not found for %s", r.URL.Path) | ||
w.WriteHeader(404) | ||
fmt.Fprint(w, "Not Found !") | ||
} | ||
} | ||
|
||
func listenAndServeWebHook(addr string) { | ||
httpHookServerMux = http.NewServeMux() | ||
commonHandlers := alice.New(loggingHandler, recoverHandler) | ||
httpHookServerMux.Handle("/", commonHandlers.ThenFunc(routerHandler)) | ||
go http.ListenAndServe(addr, httpHookServerMux) | ||
|
||
Log().Infof("Agents webHook listening on %s", addr) | ||
go http.ListenAndServe(addr, httpHookServerMux) | ||
} | ||
|
||
func loggingHandler(next http.Handler) http.Handler { | ||
fn := func(w http.ResponseWriter, r *http.Request) { | ||
next.ServeHTTP(w, r) | ||
Log().Debugf("Webhook [%s] %s", r.Method, r.URL.Path) | ||
} | ||
return http.HandlerFunc(fn) | ||
} | ||
|
||
func recoverHandler(next http.Handler) http.Handler { | ||
fn := func(w http.ResponseWriter, r *http.Request) { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
Log().Errorf("Webhook panic [%s] %s : %+v", r.Method, r.URL.Path, err) | ||
http.Error(w, http.StatusText(500), 500) | ||
} | ||
}() | ||
|
||
next.ServeHTTP(w, r) | ||
} | ||
|
||
return http.HandlerFunc(fn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.