Created
April 30, 2015 15:50
-
-
Save deankarn/ccb8f57b46f770f8a10a to your computer and use it in GitHub Desktop.
Wrapping/implementing golang's http.ResponseWriter example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LogResponseWritter wraps the standard http.ResponseWritter allowing for more | |
// verbose logging | |
type LogResponseWritter struct { | |
status int | |
size int | |
http.ResponseWriter | |
} | |
// func NewMyResponseWriter(res http.ResponseWriter) *MyResponseWriter { | |
// // Default the status code to 200 | |
// return &MyResponseWriter{200, res} | |
// } | |
// Status provides an easy way to retrieve the status code | |
func (w *LogResponseWritter) Status() int { | |
return w.status | |
} | |
// Size provides an easy way to retrieve the response size in bytes | |
func (w *LogResponseWritter) Size() int { | |
return w.size | |
} | |
// Header returns & satisfies the http.ResponseWriter interface | |
func (w *LogResponseWritter) Header() http.Header { | |
return w.ResponseWriter.Header() | |
} | |
// Write satisfies the http.ResponseWriter interface and | |
// captures data written, in bytes | |
func (w *LogResponseWritter) Write(data []byte) (int, error) { | |
written, err := w.ResponseWriter.Write(data) | |
w.size += written | |
return written, err | |
} | |
// WriteHeader satisfies the http.ResponseWriter interface and | |
// allows us to cach the status code | |
func (w *LogResponseWritter) WriteHeader(statusCode int) { | |
w.status = statusCode | |
w.ResponseWriter.WriteHeader(statusCode) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment