You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

heartbeat.go 731B

1234567891011121314151617181920212223242526
  1. package middleware
  2. import (
  3. "net/http"
  4. "strings"
  5. )
  6. // Heartbeat endpoint middleware useful to setting up a path like
  7. // `/ping` that load balancers or uptime testing external services
  8. // can make a request before hitting any routes. It's also convenient
  9. // to place this above ACL middlewares as well.
  10. func Heartbeat(endpoint string) func(http.Handler) http.Handler {
  11. f := func(h http.Handler) http.Handler {
  12. fn := func(w http.ResponseWriter, r *http.Request) {
  13. if r.Method == "GET" && strings.EqualFold(r.URL.Path, endpoint) {
  14. w.Header().Set("Content-Type", "text/plain")
  15. w.WriteHeader(http.StatusOK)
  16. w.Write([]byte("."))
  17. return
  18. }
  19. h.ServeHTTP(w, r)
  20. }
  21. return http.HandlerFunc(fn)
  22. }
  23. return f
  24. }