Extract internal router creation from server

This commit is contained in:
SALLEYRON Julien 2018-04-23 15:30:03 +02:00 committed by Traefiker Bot
parent 05968eb232
commit 9daae9c705
8 changed files with 697 additions and 254 deletions

View file

@ -1,9 +1,9 @@
package ping
import (
"context"
"fmt"
"net/http"
"sync"
"github.com/containous/mux"
)
@ -12,26 +12,22 @@ import (
type Handler struct {
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
// WithContext causes the ping endpoint to serve non 200 responses.
func (h *Handler) WithContext(ctx context.Context) {
go func() {
<-ctx.Done()
h.terminating = true
}()
}
// AddRoutes add ping routes on a router
func (g *Handler) AddRoutes(router *mux.Router) {
func (h *Handler) AddRoutes(router *mux.Router) {
router.Methods(http.MethodGet, http.MethodHead).Path("/ping").
HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
g.lock.RLock()
defer g.lock.RUnlock()
statusCode := http.StatusOK
if g.terminating {
if h.terminating {
statusCode = http.StatusServiceUnavailable
}
response.WriteHeader(statusCode)