Skip to content

Commit

Permalink
Defining two "health" endpoints, both / and /health, makes k8s happy
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynom committed Apr 27, 2017
1 parent 643b8a5 commit 05fc1f0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion handlers/ratelimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func NewRateLimitHandler(l log.Logger, b *ratelimit.Bucket) func(h http.Handler)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
d := b.Take(1)
if d > 0 {
l.Log("msg", "Rate limiting", "delay", d)
l.Log("msg", "Rate limiting", "delay", d, "remote_addr", r.RemoteAddr)
time.Sleep(d)
}

Expand Down
7 changes: 3 additions & 4 deletions handlers/statuspaths.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ import (
)

func NewHTTPStatusPaths(_ log.Logger, paths []string, httpStatus int) func(h http.Handler) http.Handler {
var actions = make(map[string]bool, len(paths))
var pathMap = make(map[string]bool, len(paths))
for _, p := range paths {
if p == "" {
continue
}

actions[p] = true
pathMap[p] = true
}

return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
action := r.URL.Path[1:]
if _, exists := actions[action]; action != "" && exists {
if _, exists := pathMap[r.URL.Path]; exists {
w.WriteHeader(httpStatus)
return
}
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ func decorateHandler(l log.Logger, h http.Handler, b *ratelimit.Bucket) http.Han
decorators = append(
decorators,

handlers.NewHTTPStatusPaths(l, []string{"health", "health/"}, http.StatusOK),
// Defining "health" end-points.
handlers.NewHTTPStatusPaths(l, []string{"/health", "/"}, http.StatusOK),

// Ignoring common foo requests
handlers.NewHTTPStatusPaths(l, []string{"/favicon"}, http.StatusNotFound),
Expand Down

0 comments on commit 05fc1f0

Please sign in to comment.