From ffd8afad074f5d8e4ff0b20fe2c4f985dd0edcf6 Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 13 Apr 2021 00:52:06 -0500 Subject: [PATCH] change both /health and /ping to exact url match needed to prevent conflict with proxied services implementing /health or /ping --- app/proxy/health.go | 17 ++++++++++++++++- app/proxy/proxy.go | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/proxy/health.go b/app/proxy/health.go index e3ab20c..67e064b 100644 --- a/app/proxy/health.go +++ b/app/proxy/health.go @@ -16,7 +16,7 @@ import ( func (h *Http) healthMiddleware(next http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { - if r.Method == "GET" && strings.HasSuffix(strings.ToLower(r.URL.Path), "/health") { + if r.Method == "GET" && strings.ToLower(r.URL.Path) == "/health" { h.healthHandler(w, r) return } @@ -89,3 +89,18 @@ func (h *Http) healthHandler(w http.ResponseWriter, _ *http.Request) { log.Printf("[WARN] failed to send halth, %v", err) } } + +// pingHandler middleware response with pong to /ping. Stops chain if ping request detected +func (h *Http) pingHandler(next http.Handler) http.Handler { + fn := func(w http.ResponseWriter, r *http.Request) { + + if r.Method == "GET" && strings.ToLower(r.URL.Path) == "/ping" { + w.Header().Set("Content-Type", "text/plain") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte("pong")) + return + } + next.ServeHTTP(w, r) + } + return http.HandlerFunc(fn) +} diff --git a/app/proxy/proxy.go b/app/proxy/proxy.go index c438e22..e207570 100644 --- a/app/proxy/proxy.go +++ b/app/proxy/proxy.go @@ -85,7 +85,7 @@ func (h *Http) Run(ctx context.Context) error { handler := R.Wrap(h.proxyHandler(), R.Recoverer(log.Default()), h.signatureHandler(), - R.Ping, + h.pingHandler, h.healthMiddleware, h.accessLogHandler(h.AccessLog), R.SizeLimit(h.MaxBodySize),