middleware/ping.go

22 lines
518 B
Go
Raw Normal View History

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