reprogl
1package http2
3import (4"fmt"5base "net/http"6"runtime"7"time"8
9"xelbot.com/reprogl/container"10)
11
12type LogResponseWriter interface {13base.ResponseWriter14Status() int15Duration() time.Duration16}
17
18type Response struct {19base.ResponseWriter20StatusCode int21
22start time.Time23}
24
25func CreateLogResponse(w base.ResponseWriter) *Response {26return &Response{27ResponseWriter: w,28start: time.Now(),29}30}
31
32func (lrw *Response) WriteHeader(statusCode int) {33lrw.StatusCode = statusCode34lrw.ResponseWriter.WriteHeader(statusCode)35}
36
37func (lrw *Response) Write(body []byte) (int, error) {38if _, ok := lrw.ResponseWriter.Header()["Cache-Control"]; !ok {39lrw.ResponseWriter.Header().Set("Cache-Control", "private, no-cache, max-age=0")40}41
42lrw.Header().Set("X-Powered-By", fmt.Sprintf(43"Reprogl/%s (%s)",44container.Version,45runtime.Version()))46
47return lrw.ResponseWriter.Write(body)48}
49
50func (lrw *Response) Status() int {51if lrw.StatusCode == 0 {52return base.StatusOK53}54
55return lrw.StatusCode56}
57
58func (lrw *Response) Duration() time.Duration {59return time.Since(lrw.start)60}
61