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