@@ -315,12 +315,10 @@ func RunHTTPServer(ctx context.Context, ctxCanceler func(), address, locality st
315315 multiRouter .Routers = append (multiRouter .Routers , & scdV1Router )
316316 }
317317
318- handler := logging .HTTPMiddleware (logger , * dumpRequests ,
319- healthyEndpointMiddleware (logger ,
320- & multiRouter ,
321- ))
322-
323- handler = authDecoderMiddleware (authorizer , handler )
318+ // the middlewares are wrapped and, therefore, executed in the opposite order
319+ handler := healthyEndpointMiddleware (logger , & multiRouter )
320+ handler = logging .HTTPMiddleware (logger , * dumpRequests , handler )
321+ handler = authMiddleware (authorizer , handler )
324322
325323 httpServer := & http.Server {
326324 Addr : address ,
@@ -382,8 +380,8 @@ func healthyEndpointMiddleware(logger *zap.Logger, next http.Handler) http.Handl
382380 })
383381}
384382
385- // authDecoderMiddleware decodes the authentication token and adds the Subject claim to the context.
386- func authDecoderMiddleware (authorizer * auth.Authorizer , handler http.Handler ) http.Handler {
383+ // authMiddleware decodes the authentication token and passes the claims to the context.
384+ func authMiddleware (authorizer * auth.Authorizer , handler http.Handler ) http.Handler {
387385 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
388386 var ctx context.Context
389387 claims , err := authorizer .ExtractClaims (r )
@@ -400,13 +398,13 @@ func authDecoderMiddleware(authorizer *auth.Authorizer, handler http.Handler) ht
400398
401399 var errMsg string
402400 if err != nil {
401+ //remove the stacktrace using the formatting specifier "%#s"
403402 errMsg = fmt .Sprintf ("%#s" , err )
404403 }
405404
406405 ctx = context .WithValue (ctx , logging.CtxAuthKey {}, logging.CtxAuthValue {
407406 Subject : claims .Subject ,
408- //remove the stacktrace using the formatting specifier "%#s"
409- ErrMsg : errMsg ,
407+ ErrMsg : errMsg ,
410408 })
411409
412410 handler .ServeHTTP (w , r .WithContext (ctx ))
0 commit comments