middleware/ping.go
2020-09-30 19:40:06 +03:00

24 lines
498 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 {
fn := 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
return
}
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}