...

Source file src/github.com/go-chi/chi/middleware/heartbeat.go

Documentation: github.com/go-chi/chi/middleware

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

View as plain text