Skip to content

add support for retrieving total bytes written in response #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion rest/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package rest

import (
"net/http"

"github.com/miolini/datacounter"
)

// HandlerFunc defines the handler function. It is the go-json-rest equivalent of http.HandlerFunc.
Expand Down Expand Up @@ -61,8 +63,10 @@ func adapterFunc(handler HandlerFunc) http.HandlerFunc {
map[string]interface{}{},
}

countingWriter := datacounter.NewResponseWriterCounter(origWriter)

writer := &responseWriter{
origWriter,
countingWriter,
false,
}

Expand Down
9 changes: 9 additions & 0 deletions rest/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"net"
"net/http"

"github.com/miolini/datacounter"
)

// A ResponseWriter interface dedicated to JSON HTTP response.
Expand All @@ -27,6 +29,9 @@ type ResponseWriter interface {
// Similar to the http.ResponseWriter interface, with additional JSON related
// headers set.
WriteHeader(int)

// Count of bytes written as response
Count() uint64
}

// This allows to customize the field name used in the error response payload.
Expand Down Expand Up @@ -125,3 +130,7 @@ func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hijacker := w.ResponseWriter.(http.Hijacker)
return hijacker.Hijack()
}

func (w *responseWriter) Count() uint64 {
return w.ResponseWriter.(*datacounter.ResponseWriterCounter).Count()
}