2017-11-09 15:12:04 +00:00
|
|
|
package ping
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2018-03-22 17:18:03 +00:00
|
|
|
"sync"
|
2017-11-09 15:12:04 +00:00
|
|
|
|
|
|
|
"github.com/containous/mux"
|
|
|
|
)
|
|
|
|
|
2018-03-22 17:18:03 +00:00
|
|
|
// Handler expose ping routes
|
2017-11-09 15:12:04 +00:00
|
|
|
type Handler struct {
|
2018-03-22 17:18:03 +00:00
|
|
|
EntryPoint string `description:"Ping entryPoint" export:"true"`
|
|
|
|
terminating bool
|
|
|
|
lock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTerminating causes the ping endpoint to serve non 200 responses.
|
|
|
|
func (g *Handler) SetTerminating() {
|
|
|
|
g.lock.Lock()
|
|
|
|
defer g.lock.Unlock()
|
|
|
|
|
|
|
|
g.terminating = true
|
2017-11-09 15:12:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddRoutes add ping routes on a router
|
2018-03-22 17:18:03 +00:00
|
|
|
func (g *Handler) AddRoutes(router *mux.Router) {
|
2017-11-20 08:40:03 +00:00
|
|
|
router.Methods(http.MethodGet, http.MethodHead).Path("/ping").
|
2017-11-09 15:12:04 +00:00
|
|
|
HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
2018-03-22 17:18:03 +00:00
|
|
|
g.lock.RLock()
|
|
|
|
defer g.lock.RUnlock()
|
|
|
|
|
|
|
|
statusCode := http.StatusOK
|
|
|
|
if g.terminating {
|
|
|
|
statusCode = http.StatusServiceUnavailable
|
|
|
|
}
|
|
|
|
response.WriteHeader(statusCode)
|
|
|
|
fmt.Fprint(response, http.StatusText(statusCode))
|
2017-11-09 15:12:04 +00:00
|
|
|
})
|
|
|
|
}
|