diff options
author | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-01-07 19:37:59 +0300 |
---|---|---|
committer | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-01-07 19:37:59 +0300 |
commit | d7dc7ed7201437a3f450bd783800386e01d62661 (patch) | |
tree | 8053829c7ba64208917aaf599a912483a4509b95 /logging.go | |
parent | 61debe70052f5da9e32acdb4d695ccf56991df9f (diff) |
Add simple GitLab Pages daemon with custom CNAME and TLS support
Diffstat (limited to 'logging.go')
-rw-r--r-- | logging.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/logging.go b/logging.go new file mode 100644 index 00000000..bee171b2 --- /dev/null +++ b/logging.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "net/http" + "time" +) + +type loggingResponseWriter struct { + rw http.ResponseWriter + status int + written int64 + started time.Time +} + +func newLoggingResponseWriter(rw http.ResponseWriter) loggingResponseWriter { + return loggingResponseWriter{ + rw: rw, + started: time.Now(), + } +} + +func (l *loggingResponseWriter) Header() http.Header { + return l.rw.Header() +} + +func (l *loggingResponseWriter) Write(data []byte) (n int, err error) { + if l.status == 0 { + l.WriteHeader(http.StatusOK) + } + n, err = l.rw.Write(data) + l.written += int64(n) + return +} + +func (l *loggingResponseWriter) WriteHeader(status int) { + if l.status != 0 { + return + } + + l.status = status + l.rw.WriteHeader(status) +} + +func (l *loggingResponseWriter) Log(r *http.Request) { + duration := time.Since(l.started) + fmt.Printf("%s %s - - [%s] %q %d %d %q %q %f\n", + r.Host, r.RemoteAddr, l.started, + fmt.Sprintf("%s %s %s", r.Method, r.RequestURI, r.Proto), + l.status, l.written, r.Referer(), r.UserAgent(), duration.Seconds(), + ) +} |