Skip to content

Commit 580d6b6

Browse files
committed
Introducing fixed routes that return a fixed HTTP status.
1 parent c5aa25a commit 580d6b6

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

handlers/statuspaths.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package handlers
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/go-kit/kit/log"
7+
)
8+
9+
func NewHTTPStatusPaths(_ log.Logger, paths []string, httpStatus int) func(h http.Handler) http.Handler {
10+
var actions = make(map[string]bool, len(paths))
11+
for _, p := range paths {
12+
if p == "" {
13+
continue
14+
}
15+
16+
actions[p] = true
17+
}
18+
19+
return func(h http.Handler) http.Handler {
20+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
21+
action := r.URL.Path[1:]
22+
if _, exists := actions[action]; action != "" && exists {
23+
w.WriteHeader(httpStatus)
24+
return
25+
}
26+
27+
h.ServeHTTP(w, r)
28+
})
29+
}
30+
}

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ func decorateHandler(l log.Logger, h http.Handler, b *ratelimit.Bucket) http.Han
135135
// Defining early needed handlers last
136136
decorators = append(
137137
decorators,
138+
139+
// Checking on the route "health", doesn't support path-segment-stripping!
140+
handlers.NewHTTPStatusPaths(l, []string{"health"}, http.StatusOK),
141+
138142
handlers.NewIgnoreFaviconRequests(),
139143
handlers.NewRateLimitHandler(l, b),
140144
)

0 commit comments

Comments
 (0)