diff --git a/.travis.yml b/.travis.yml index d44db9f..d58668b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,4 +3,6 @@ language: go go: - 1.3 - 1.4 - - 1.5.2 + - 1.5 + - 1.6 + - 1.7 diff --git a/README.md b/README.md index 69a9ced..8979ec1 100644 --- a/README.md +++ b/README.md @@ -556,7 +556,7 @@ type Reminder struct { } type Impl struct { - DB gorm.DB + DB *gorm.DB } func (i *Impl) InitDB() { diff --git a/rest/access_log_apache.go b/rest/access_log_apache.go index 72761f8..d82894a 100644 --- a/rest/access_log_apache.go +++ b/rest/access_log_apache.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "log" + "net" "os" "strings" "text/template" @@ -131,7 +132,10 @@ func (mw *AccessLogApacheMiddleware) convertFormat() { return fmt.Sprintf("%d", value) }, "microseconds": func(dur *time.Duration) string { - return fmt.Sprintf("%d", dur.Nanoseconds()/1000) + if dur != nil { + return fmt.Sprintf("%d", dur.Nanoseconds()/1000) + } + return "" }, "statusCodeColor": func(statusCode int) string { if statusCode >= 400 && statusCode < 500 { @@ -195,8 +199,9 @@ func (u *accessLogUtil) StartTime() *time.Time { func (u *accessLogUtil) ApacheRemoteAddr() string { remoteAddr := u.R.RemoteAddr if remoteAddr != "" { - parts := strings.SplitN(remoteAddr, ":", 2) - return parts[0] + if ip, _, err := net.SplitHostPort(remoteAddr); err == nil { + return ip + } } return "" } diff --git a/rest/gzip.go b/rest/gzip.go index 10f4e9d..0fafc05 100644 --- a/rest/gzip.go +++ b/rest/gzip.go @@ -21,6 +21,12 @@ func (mw *GzipMiddleware) MiddlewareFunc(h HandlerFunc) HandlerFunc { canGzip := strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") // client accepts gzip ? writer := &gzipResponseWriter{w, false, canGzip, nil} + defer func() { + // need to close gzip writer + if writer.gzipWriter != nil { + writer.gzipWriter.Close() + } + }() // call the handler with the wrapped writer h(writer, r) } diff --git a/rest/recorder_test.go b/rest/recorder_test.go index c02b846..ebb07f8 100644 --- a/rest/recorder_test.go +++ b/rest/recorder_test.go @@ -67,8 +67,8 @@ func TestRecorderAndGzipMiddleware(t *testing.T) { } bytesWritten := r.Env["BYTES_WRITTEN"].(int64) // Yes, the gzipped version actually takes more space. - if bytesWritten != 28 { - t.Errorf("BYTES_WRITTEN 28 expected, got %d", bytesWritten) + if bytesWritten != 41 { + t.Errorf("BYTES_WRITTEN 41 expected, got %d", bytesWritten) } } }))