From 580d6b6971a5d36ea5306d41a827e71c3725346b Mon Sep 17 00:00:00 2001 From: Mark van der Velden Date: Wed, 26 Apr 2017 16:54:51 +0200 Subject: [PATCH] Introducing fixed routes that return a fixed HTTP status. --- handlers/statuspaths.go | 30 ++++++++++++++++++++++++++++++ main.go | 4 ++++ 2 files changed, 34 insertions(+) create mode 100644 handlers/statuspaths.go diff --git a/handlers/statuspaths.go b/handlers/statuspaths.go new file mode 100644 index 0000000..dc1f434 --- /dev/null +++ b/handlers/statuspaths.go @@ -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) + }) + } +} diff --git a/main.go b/main.go index 20dd3de..53269dd 100644 --- a/main.go +++ b/main.go @@ -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), )