2015-01-14 03:43:45 +08:00
|
|
|
package middleware
|
|
|
|
|
2015-04-24 03:35:56 +08:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
2015-01-14 03:43:45 +08:00
|
|
|
|
|
|
|
// responseRecorder is a type of ResponseWriter that captures
|
|
|
|
// the status code written to it and also the size of the body
|
|
|
|
// written in the response. A status code does not have
|
|
|
|
// to be written, however, in which case 200 must be assumed.
|
|
|
|
// It is best to have the constructor initialize this type
|
|
|
|
// with that default status code.
|
|
|
|
type responseRecorder struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
status int
|
|
|
|
size int
|
2015-04-24 03:35:56 +08:00
|
|
|
start time.Time
|
2015-01-14 03:43:45 +08:00
|
|
|
}
|
|
|
|
|
2015-01-30 13:06:53 +08:00
|
|
|
// NewResponseRecorder makes and returns a new responseRecorder,
|
2015-01-14 03:43:45 +08:00
|
|
|
// which captures the HTTP Status code from the ResponseWriter
|
|
|
|
// and also the length of the response body written through it.
|
|
|
|
// Because a status is not set unless WriteHeader is called
|
|
|
|
// explicitly, this constructor initializes with a status code
|
|
|
|
// of 200 to cover the default case.
|
2015-01-30 13:06:53 +08:00
|
|
|
func NewResponseRecorder(w http.ResponseWriter) *responseRecorder {
|
2015-01-14 03:43:45 +08:00
|
|
|
return &responseRecorder{
|
|
|
|
ResponseWriter: w,
|
|
|
|
status: http.StatusOK,
|
2015-04-24 03:35:56 +08:00
|
|
|
start: time.Now(),
|
2015-01-14 03:43:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteHeader records the status code and calls the
|
|
|
|
// underlying ResponseWriter's WriteHeader method.
|
|
|
|
func (r *responseRecorder) WriteHeader(status int) {
|
|
|
|
r.status = status
|
|
|
|
r.ResponseWriter.WriteHeader(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write is a wrapper that records the size of the body
|
|
|
|
// that gets written.
|
|
|
|
func (r *responseRecorder) Write(buf []byte) (int, error) {
|
|
|
|
n, err := r.ResponseWriter.Write(buf)
|
|
|
|
if err == nil {
|
|
|
|
r.size += n
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|