Skip to content
This repository has been archived by the owner on Mar 15, 2022. It is now read-only.

Commit

Permalink
Handler now accepts customizable routes for liveness and readiness
Browse files Browse the repository at this point in the history
  • Loading branch information
Win Hung Chan authored and Win Hung Chan committed Apr 24, 2018
1 parent da5fdee commit 49820fe
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ See the [GoDoc examples](https://godoc.org/github.com/heptiolabs/healthcheck) fo

- Create a `healthcheck.Handler`:
```go
health := healthcheck.NewHandler()
health := healthcheck.NewHandler("", "")
```

- Configure some application-specific liveness checks (whether the app itself is unhealthy):
Expand Down
6 changes: 3 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

func Example() {
// Create a Handler that we can use to register liveness and readiness checks.
health := NewHandler()
health := NewHandler("", "")

// Add a readiness check to make sure an upstream dependency resolves in DNS.
// If this fails we don't want to receive requests, but we shouldn't be
Expand Down Expand Up @@ -65,7 +65,7 @@ func Example_database() {
database = connectToDatabase()

// Create a Handler that we can use to register liveness and readiness checks.
health := NewHandler()
health := NewHandler("", "")

// Add a readiness check to we don't receive requests unless we can reach
// the database with a ping in <1 second.
Expand All @@ -89,7 +89,7 @@ func Example_database() {

func Example_advanced() {
// Create a Handler that we can use to register liveness and readiness checks.
health := NewHandler()
health := NewHandler("", "")

// Make sure we can connect to an upstream dependency over TCP in less than
// 50ms. Run this check asynchronously in the background every 10 seconds
Expand Down
15 changes: 12 additions & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,22 @@ type basicHandler struct {
}

// NewHandler creates a new basic Handler
func NewHandler() Handler {
func NewHandler(liveURL string, readyURL string) Handler {
h := &basicHandler{
livenessChecks: make(map[string]Check),
readinessChecks: make(map[string]Check),
}
h.Handle("/live", http.HandlerFunc(h.LiveEndpoint))
h.Handle("/ready", http.HandlerFunc(h.ReadyEndpoint))

if liveURL == "" {
liveURL = "/live"
}

if readyURL == "" {
readyURL = "/ready"
}

h.Handle(liveURL, http.HandlerFunc(h.LiveEndpoint))
h.Handle(readyURL, http.HandlerFunc(h.ReadyEndpoint))
return h
}

Expand Down
2 changes: 1 addition & 1 deletion handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestNewHandler(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := NewHandler()
h := NewHandler("", "")

if !tt.live {
h.AddLivenessCheck("test-liveness-check", func() error {
Expand Down
2 changes: 1 addition & 1 deletion metrics_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type metricsHandler struct {
// into the provided Prometheus registry.
func NewMetricsHandler(registry prometheus.Registerer, namespace string) Handler {
return &metricsHandler{
handler: NewHandler(),
handler: NewHandler("", ""),
registry: registry,
namespace: namespace,
}
Expand Down

0 comments on commit 49820fe

Please sign in to comment.