Improve cacheRecorder, don't log ping requests from healthcheck

This commit is contained in:
Jan-Lukas Else 2022-02-21 22:02:33 +01:00
parent 46548df638
commit 505641170e
2 changed files with 23 additions and 28 deletions

View File

@ -1,64 +1,58 @@
package main package main
import ( import (
"fmt"
"net/http" "net/http"
) )
// cacheRecorder is an implementation of http.ResponseWriter // cacheRecorder is an implementation of http.ResponseWriter
type cacheRecorder struct { type cacheRecorder struct {
item *cacheItem item cacheItem
done bool
} }
func newCacheRecorder() *cacheRecorder { func newCacheRecorder() *cacheRecorder {
return &cacheRecorder{ return &cacheRecorder{
item: &cacheItem{ item: cacheItem{
code: http.StatusOK, code: http.StatusOK,
header: make(http.Header), header: http.Header{},
}, },
} }
} }
func (c *cacheRecorder) finish() (ci *cacheItem) { func (c *cacheRecorder) finish() *cacheItem {
ci = c.item c.done = true
c.item = nil return &c.item
return
} }
// Header implements http.ResponseWriter. // Header implements http.ResponseWriter.
func (rw *cacheRecorder) Header() http.Header { func (c *cacheRecorder) Header() http.Header {
if rw.item == nil { if c.done {
return nil return nil
} }
return rw.item.header return c.item.header
} }
// Write implements http.ResponseWriter. // Write implements http.ResponseWriter.
func (rw *cacheRecorder) Write(buf []byte) (int, error) { func (c *cacheRecorder) Write(buf []byte) (int, error) {
if rw.item == nil { if c.done {
return 0, nil return 0, nil
} }
rw.item.body = append(rw.item.body, buf...) c.item.body = append(c.item.body, buf...)
return len(buf), nil return len(buf), nil
} }
// WriteString implements io.StringWriter. // WriteString implements io.StringWriter.
func (rw *cacheRecorder) WriteString(str string) (int, error) { func (c *cacheRecorder) WriteString(str string) (int, error) {
return rw.Write([]byte(str)) if c.done {
return 0, nil
}
return c.Write([]byte(str))
} }
// WriteHeader implements http.ResponseWriter. // WriteHeader implements http.ResponseWriter.
func (rw *cacheRecorder) WriteHeader(code int) { func (c *cacheRecorder) WriteHeader(code int) {
if rw.item == nil { if c.done {
return return
} }
if code < 100 || code > 999 { c.item.code = code
panic(fmt.Sprintf("invalid WriteHeader code %v", code))
}
rw.item.code = code
}
// Flush implements http.Flusher.
func (rw *cacheRecorder) Flush() {
// Do nothing
} }

View File

@ -38,10 +38,11 @@ func (a *goBlog) startServer() (err error) {
} }
// Set basic middlewares // Set basic middlewares
h := alice.New() h := alice.New()
h = h.Append(middleware.Heartbeat("/ping"))
if a.cfg.Server.Logging { if a.cfg.Server.Logging {
h = h.Append(a.logMiddleware) h = h.Append(a.logMiddleware)
} }
h = h.Append(middleware.Recoverer, httpcompress.Compress(flate.BestCompression), middleware.Heartbeat("/ping")) h = h.Append(middleware.Recoverer, httpcompress.Compress(flate.BestCompression))
if a.httpsConfigured(false) { if a.httpsConfigured(false) {
h = h.Append(a.securityHeaders) h = h.Append(a.securityHeaders)
} }