-
Notifications
You must be signed in to change notification settings - Fork 5k
/
ping.go
40 lines (33 loc) · 895 Bytes
/
ping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package ping
import (
"fmt"
"net/http"
"sync"
"github.com/containous/mux"
)
// Handler expose ping routes
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
}
// AddRoutes add ping routes on a router
func (g *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 {
statusCode = http.StatusServiceUnavailable
}
response.WriteHeader(statusCode)
fmt.Fprint(response, http.StatusText(statusCode))
})
}