middleware/ping.go
2023-12-25 03:47:26 +03:00

22 lines
518 B
Go

package middleware
import (
"net/http"
"strings"
)
// Ping responses with pong to /ping request and stops chain
func Ping(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && strings.HasSuffix(strings.ToLower(r.URL.Path), "/ping") {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
w.Write([]byte("pong")) // nolint:errcheck // No need to check error here
return
}
next.ServeHTTP(w, r)
})
}