Skip to content

Commit

Permalink
minor changes based on PR feedback
Browse files Browse the repository at this point in the history
* remove parameter for httpPingOnly
* remove debug message on httpPingOnly
* use const 'pingPath' consistently

Signed-off-by: Dmitry Savintsev <[email protected]>
  • Loading branch information
dmitris committed Sep 27, 2023
1 parent bae3734 commit b09fff4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
1 change: 0 additions & 1 deletion cmd/timestamp-server/app/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ var serveCmd = &cobra.Command{
host := viper.GetString("host")
port := int(viper.GetUint("port"))
scheme := viper.GetStringSlice("scheme")
httpPingOnly := viper.GetBool("http-ping-only")
server := server.NewRestAPIServer(host, port, scheme, httpPingOnly, readTimeout, writeTimeout)
defer func() {
if err := server.Shutdown(); err != nil {
Expand Down
12 changes: 7 additions & 5 deletions pkg/generated/restapi/configure_timestamp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,17 @@ func (l *logAdapter) Print(v ...interface{}) {
log.Logger.Info(v...)
}

const pingPath = "/ping"

// httpPingOnly custom middleware prohibits all entrypoints except
// "/ping" on the http (non-HTTPS) server.
func httpPingOnly(endpoint string) func(http.Handler) http.Handler {
func httpPingOnly() 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) {
if r.URL.Scheme != "https" && !strings.EqualFold(r.URL.Path, pingPath) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("http server supports only the /ping entrypoint")) //nolint:errcheck
w.Write([]byte("http server supports only the " + pingPath + " entrypoint")) //nolint:errcheck
return
}
h.ServeHTTP(w, r)
Expand All @@ -128,9 +130,9 @@ func setupGlobalMiddleware(handler http.Handler) http.Handler {
&middleware.DefaultLogFormatter{Logger: &logAdapter{}})
returnHandler := middleware.Logger(handler)
returnHandler = middleware.Recoverer(returnHandler)
returnHandler = middleware.Heartbeat("/ping")(returnHandler)
returnHandler = middleware.Heartbeat(pingPath)(returnHandler)
if cmdparams.IsHTTPPingOnly {
returnHandler = httpPingOnly("/ping")(returnHandler)
returnHandler = httpPingOnly()(returnHandler)
}

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

0 comments on commit b09fff4

Please sign in to comment.