Skip to content

Commit

Permalink
add httpPingOnly middleware
Browse files Browse the repository at this point in the history
Add a small custom middlerware httpPingOnly to allow only
/ping entrypoint for the http (non-https) server.

Related to issue #420.

Signed-off-by: Dmitry S <[email protected]>
  • Loading branch information
dmitris committed Sep 18, 2023
1 parent 473cf2a commit d388e35
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/generated/restapi/configure_timestamp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"crypto/tls"
"net/http"
"strconv"
"strings"
"time"

"github.com/go-chi/chi/middleware"
Expand Down Expand Up @@ -101,6 +102,24 @@ func (l *logAdapter) Print(v ...interface{}) {
log.Logger.Info(v...)
}

// httpPingOnly custom middleware prohibits all entrypoing except
// "/ping" on the http (non-HTTPS) server.
func httpPingOnly(endpoint string) func(http.Handler) http.Handler {
f := func(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if r.URL.Scheme != "https" && !strings.EqualFold(r.URL.Path, endpoint) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("http server supports only the /ping entrypoint"))
return
}
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
return f
}

// The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document.
// So this is a good place to plug in a panic handling middleware, logging and metrics.
func setupGlobalMiddleware(handler http.Handler) http.Handler {
Expand All @@ -109,6 +128,7 @@ func setupGlobalMiddleware(handler http.Handler) http.Handler {
returnHandler := middleware.Logger(handler)
returnHandler = middleware.Recoverer(returnHandler)
returnHandler = middleware.Heartbeat("/ping")(returnHandler)
returnHandler = httpPingOnly("/ping")(returnHandler)

handleCORS := cors.Default().Handler
returnHandler = handleCORS(returnHandler)
Expand Down

0 comments on commit d388e35

Please sign in to comment.