change both /health and /ping to exact url match

needed to prevent conflict with proxied services
implementing /health or /ping
This commit is contained in:
Umputun
2021-04-13 00:52:06 -05:00
parent 21636b8a4b
commit ffd8afad07
2 changed files with 17 additions and 2 deletions

View File

@@ -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)
}

View File

@@ -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),