Skip to content

Commit

Permalink
Introducing fixed routes that return a fixed HTTP status.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynom committed Apr 26, 2017
1 parent c5aa25a commit 580d6b6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
30 changes: 30 additions & 0 deletions handlers/statuspaths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package handlers

import (
"net/http"

"github.com/go-kit/kit/log"
)

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

actions[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 {
w.WriteHeader(httpStatus)
return
}

h.ServeHTTP(w, r)
})
}
}
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ func decorateHandler(l log.Logger, h http.Handler, b *ratelimit.Bucket) http.Han
// Defining early needed handlers last
decorators = append(
decorators,

// Checking on the route "health", doesn't support path-segment-stripping!
handlers.NewHTTPStatusPaths(l, []string{"health"}, http.StatusOK),

handlers.NewIgnoreFaviconRequests(),
handlers.NewRateLimitHandler(l, b),
)
Expand Down

0 comments on commit 580d6b6

Please sign in to comment.