34 lines
804 B
Go
34 lines
804 B
Go
package http
|
|
|
|
import (
|
|
"net/http"
|
|
)
|
|
|
|
// statusCaptureWriter is a ResponseWriter that captures the status code
|
|
type statusCaptureWriter struct {
|
|
http.ResponseWriter
|
|
statusCode int
|
|
}
|
|
|
|
// WriteHeader captures the status code and passes it to the wrapped ResponseWriter
|
|
func (w *statusCaptureWriter) WriteHeader(code int) {
|
|
w.statusCode = code
|
|
w.ResponseWriter.WriteHeader(code)
|
|
}
|
|
|
|
// StatusCode returns the captured status code
|
|
func (w *statusCaptureWriter) StatusCode() int {
|
|
if w.statusCode == 0 {
|
|
return http.StatusOK // Default to 200 if not explicitly set
|
|
}
|
|
return w.statusCode
|
|
}
|
|
|
|
// newStatusCaptureWriter creates a new statusCaptureWriter
|
|
func newStatusCaptureWriter(w http.ResponseWriter) *statusCaptureWriter {
|
|
return &statusCaptureWriter{
|
|
ResponseWriter: w,
|
|
statusCode: 0,
|
|
}
|
|
}
|