50 lines
1.1 KiB
Go
Raw Permalink Normal View History

2024-03-22 14:08:24 +00:00
package web
2024-03-16 14:22:47 +00:00
import (
"io"
"net/http"
)
// Response is the interface for an HTTP response.
type Response interface {
2024-03-22 13:37:21 +00:00
Flush()
2024-03-16 14:22:47 +00:00
Header(key string) string
SetHeader(key string, value string)
Write([]byte) (int, error)
WriteString(string) (int, error)
}
// response represents the HTTP response used in the given context.
type response struct {
http.ResponseWriter
}
2024-03-22 13:37:21 +00:00
// Flush flushes the response buffers to the client.
func (res response) Flush() {
flusher, ok := res.ResponseWriter.(http.Flusher)
if ok {
flusher.Flush()
}
}
2024-03-16 14:22:47 +00:00
// Header returns the header value for the given key.
func (res response) Header(key string) string {
return res.ResponseWriter.Header().Get(key)
}
// SetHeader sets the header value for the given key.
func (res response) SetHeader(key string, value string) {
res.ResponseWriter.Header().Set(key, value)
}
// Write implements the io.Writer interface.
func (res response) Write(body []byte) (int, error) {
return res.ResponseWriter.Write(body)
}
// WriteString implements the io.StringWriter interface.
func (res response) WriteString(body string) (int, error) {
return res.ResponseWriter.(io.StringWriter).WriteString(body)
}