Skip to content

Commit

Permalink
Refactor LoggingMiddlewareWithOptions to take an Options struct value…
Browse files Browse the repository at this point in the history
… not pointer

- No strong reason for this to be a pointer,
- Makes the call site easier to understand for a reader.
- Fix cases where we called `extraHeaders` instead of `ExtraHeaders`

Signed-off-by: Lawrence Lawson <[email protected]>
  • Loading branch information
Zagan202 committed Mar 10, 2021
1 parent 20a9c90 commit 8074feb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
12 changes: 4 additions & 8 deletions support/http/logging_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ func SetLoggerMiddleware(l *log.Entry) func(stdhttp.Handler) stdhttp.Handler {

// LoggingMiddleware is a middleware that logs requests to the logger.
func LoggingMiddleware(next stdhttp.Handler) stdhttp.Handler {
return LoggingMiddlewareWithOptions(nil)(next)
return LoggingMiddlewareWithOptions(Options{})(next)
}

// LoggingMiddlewareWithOptions is a middleware that logs requests to the logger.
// Requires an Options struct to accept additional information.
func LoggingMiddlewareWithOptions(options *Options) func(stdhttp.Handler) stdhttp.Handler {
func LoggingMiddlewareWithOptions(options Options) func(stdhttp.Handler) stdhttp.Handler {
return func(next stdhttp.Handler) stdhttp.Handler {
return stdhttp.HandlerFunc(func(w stdhttp.ResponseWriter, r *stdhttp.Request) {
mw := mutil.WrapWriter(w)
Expand All @@ -46,11 +46,7 @@ func LoggingMiddlewareWithOptions(options *Options) func(stdhttp.Handler) stdhtt
})
r = r.WithContext(ctx)

extraHeaders := []string{}
if options != nil {
extraHeaders = options.extraHeaders
}
logStartOfRequest(r, extraHeaders...)
logStartOfRequest(r, options.ExtraHeaders)

then := time.Now()
next.ServeHTTP(mw, r)
Expand All @@ -65,7 +61,7 @@ func LoggingMiddlewareWithOptions(options *Options) func(stdhttp.Handler) stdhtt
// beginning processing.
func logStartOfRequest(
r *stdhttp.Request,
extraHeaders ...string,
extraHeaders []string,
) {
fields := log.F{}
for _, header := range extraHeaders {
Expand Down
3 changes: 1 addition & 2 deletions support/http/logging_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ func TestHTTPMiddlewareWithOptions(t *testing.T) {
mux.Use(setXFFMiddleware)
mux.Use(setContentMD5Middleware)
mux.Use(middleware.RequestID)
extraHeaders := []string{"X-Forwarded-For", "Content-MD5"}
options := &Options{extraHeaders: extraHeaders}
options := Options{ExtraHeaders: []string{"X-Forwarded-For", "Content-MD5"}}
mux.Use(LoggingMiddlewareWithOptions(options))

mux.Get("/path/{value}", stdhttp.HandlerFunc(func(w stdhttp.ResponseWriter, r *stdhttp.Request) {
Expand Down

0 comments on commit 8074feb

Please sign in to comment.