Simple blogging system written in Go https://goblog.app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
GoBlog/cacheRecorder.go

58 lines
1.0 KiB

package main
import (
"net/http"
)
// cacheRecorder is an implementation of http.ResponseWriter
type cacheRecorder struct {
item cacheItem
done bool
}
func newCacheRecorder() *cacheRecorder {
return &cacheRecorder{
item: cacheItem{
code: http.StatusOK,
header: http.Header{},
},
}
}
func (c *cacheRecorder) finish() *cacheItem {
c.done = true
return &c.item
}
// Header implements http.ResponseWriter.
func (c *cacheRecorder) Header() http.Header {
if c.done {
return nil
}
return c.item.header
}
// Write implements http.ResponseWriter.
func (c *cacheRecorder) Write(buf []byte) (int, error) {
if c.done {
return 0, nil
}
c.item.body = append(c.item.body, buf...)
return len(buf), nil
}
// WriteString implements io.StringWriter.
func (c *cacheRecorder) WriteString(str string) (int, error) {
if c.done {
return 0, nil
}
return c.Write([]byte(str))
}
// WriteHeader implements http.ResponseWriter.
func (c *cacheRecorder) WriteHeader(code int) {
if c.done {
return
}
c.item.code = code
}