-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker.go
More file actions
43 lines (35 loc) · 1.02 KB
/
tracker.go
File metadata and controls
43 lines (35 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package status
import "net/http"
// StatusCodeTracker wraps a ResponseWriter
// and keeps track of the returned status code
type StatusCodeTracker struct {
w http.ResponseWriter
status int
}
// Status returns the saved status code
// it should only be called once the handler has finished returned
func (t *StatusCodeTracker) Status() int {
return t.status
}
// Track a ResponseWriter for its StatusCode
func Track(w http.ResponseWriter) *StatusCodeTracker {
return &StatusCodeTracker{
w: w,
// A status code of 200 is assumed unless WriteHeader is called
status: 200,
}
}
// Header calls the underlying Header method
func (t *StatusCodeTracker) Header() http.Header {
return t.w.Header()
}
// Write calls the underlying Write method
func (t *StatusCodeTracker) Write(p []byte) (n int, err error) {
return t.w.Write(p)
}
// WriteHeader calls the underlying WriteHeader method
// but also saves the status code of the response
func (t *StatusCodeTracker) WriteHeader(code int) {
t.w.WriteHeader(code)
t.status = code
}