Extract internal router creation from server
This commit is contained in:
parent
05968eb232
commit
9daae9c705
8 changed files with 697 additions and 254 deletions
22
ping/ping.go
22
ping/ping.go
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue