traefik/pkg/ping/ping.go

42 lines
1,011 B
Go
Raw Normal View History

package ping
import (
"context"
"fmt"
"net/http"
2019-08-03 01:58:23 +00:00
"github.com/gorilla/mux"
)
2018-11-14 09:18:03 +00:00
// Handler expose ping routes.
type Handler struct {
2019-09-06 13:08:04 +00:00
EntryPoint string `description:"EntryPoint" export:"true" json:"entryPoint,omitempty" toml:"entryPoint,omitempty" yaml:"entryPoint,omitempty"`
terminating bool
}
// SetDefaults sets the default values.
func (h *Handler) SetDefaults() {
2019-09-06 13:08:04 +00:00
h.EntryPoint = "traefik"
}
// WithContext causes the ping endpoint to serve non 200 responses.
func (h *Handler) WithContext(ctx context.Context) {
go func() {
<-ctx.Done()
h.terminating = true
}()
}
2018-11-14 09:18:03 +00:00
// Append adds ping routes on a router.
func (h *Handler) Append(router *mux.Router) {
2017-11-20 08:40:03 +00:00
router.Methods(http.MethodGet, http.MethodHead).Path("/ping").
HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
statusCode := http.StatusOK
if h.terminating {
statusCode = http.StatusServiceUnavailable
}
response.WriteHeader(statusCode)
fmt.Fprint(response, http.StatusText(statusCode))
})
}