2017-11-09 16:12:04 +01:00
package ping
import (
2018-04-23 15:30:03 +02:00
"context"
2017-11-09 16:12:04 +01:00
"fmt"
"net/http"
)
2018-11-14 10:18:03 +01:00
// Handler expose ping routes.
2017-11-09 16:12:04 +01:00
type Handler struct {
2022-01-24 05:08:05 -05:00
EntryPoint string ` description:"EntryPoint" json:"entryPoint,omitempty" toml:"entryPoint,omitempty" yaml:"entryPoint,omitempty" export:"true" `
2020-10-30 12:44:05 +01:00
ManualRouting bool ` description:"Manual routing" json:"manualRouting,omitempty" toml:"manualRouting,omitempty" yaml:"manualRouting,omitempty" export:"true" `
TerminatingStatusCode int ` description:"Terminating status code" json:"terminatingStatusCode,omitempty" toml:"terminatingStatusCode,omitempty" yaml:"terminatingStatusCode,omitempty" export:"true" `
2020-07-01 14:40:04 +02:00
terminating bool
2018-03-22 10:18:03 -07:00
}
2019-06-17 11:48:05 +02:00
// SetDefaults sets the default values.
func ( h * Handler ) SetDefaults ( ) {
2019-09-06 15:08:04 +02:00
h . EntryPoint = "traefik"
2020-07-01 14:40:04 +02:00
h . TerminatingStatusCode = http . StatusServiceUnavailable
2019-06-17 11:48:05 +02:00
}
2018-04-23 15:30:03 +02:00
// WithContext causes the ping endpoint to serve non 200 responses.
func ( h * Handler ) WithContext ( ctx context . Context ) {
go func ( ) {
<- ctx . Done ( )
h . terminating = true
} ( )
2017-11-09 16:12:04 +01:00
}
2019-11-14 16:40:05 +01:00
func ( h * Handler ) ServeHTTP ( response http . ResponseWriter , request * http . Request ) {
statusCode := http . StatusOK
if h . terminating {
2020-07-01 14:40:04 +02:00
statusCode = h . TerminatingStatusCode
2019-11-14 16:40:05 +01:00
}
response . WriteHeader ( statusCode )
fmt . Fprint ( response , http . StatusText ( statusCode ) )
2017-11-09 16:12:04 +01:00
}