Skip to content

Commit

Permalink
Rework response writer interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
instabledesign committed Oct 22, 2020
1 parent 5460b92 commit 717af24
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 4 additions & 1 deletion middleware/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ func Interceptor(options ...InterceptorOption) httpware.Middleware {
config := NewInterceptorConfig(options...)
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) {
writerInterceptor := NewResponseWriterInterceptor(writer)
writerInterceptor, ok := writer.(*ResponseWriterInterceptor)
if !ok {
writerInterceptor = NewResponseWriterInterceptor(writer)
}

req.Body = interceptor.NewCopyReadCloser(req.Body)
config.CallbackBefore(writerInterceptor, req)
Expand Down
5 changes: 4 additions & 1 deletion middleware/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ func Metrics(recorder metrics.Recorder, options ... metrics.Option) httpware.Mid
config := metrics.NewConfig(recorder, options...)
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) {
writerInterceptor := NewResponseWriterInterceptor(writer)
writerInterceptor, ok := writer.(*ResponseWriterInterceptor)
if !ok {
writerInterceptor = NewResponseWriterInterceptor(writer)
}
handlerName := config.IdentifierProvider(req)
if config.MeasureInflightRequests {
config.Recorder.AddInflightRequests(req.Context(), handlerName, 1)
Expand Down

0 comments on commit 717af24

Please sign in to comment.