Documentation ¶
Overview ¶
Package httputil provides utilities for implementing web frontends and APIs with the Go standard library.
Index ¶
- Variables
- func CacheHandler(cc CacheControl, h http.Handler) http.Handler
- func DecodeJSON(r io.Reader, v interface{}) error
- func ExpectStatus(code int, allow ...int) error
- func OK(w http.ResponseWriter)
- type CacheControl
- type Error
- type ErrorHandler
- type Handler
- type HandlerFunc
- type JSONDecoder
- type JSONEncoder
- type Static
Constants ¶
This section is empty.
Variables ¶
var CacheControlImmutable = CacheControl{ MaxAge: 24 * time.Hour, SharedMaxAge: 365 * 24 * time.Hour, Directives: []string{"public", "immutable"}, }
CacheControlImmutable provides suitable cache control headers for immutable files.
var CacheControlNever = CacheControl{ MaxAge: 0, SharedMaxAge: 0, Directives: []string{"no-cache", "no-store", "no-transform", "must-revalidate", "private"}, }
CacheControlNever defines cache control headers to ensure a resource is never cached.
Functions ¶
func CacheHandler ¶
func CacheHandler(cc CacheControl, h http.Handler) http.Handler
CacheHandler wraps the handler and sets Cache-Control headers.
func DecodeJSON ¶
func ExpectStatus ¶
ExpectStatus errors if code is not in the allowed list.
func OK ¶
func OK(w http.ResponseWriter)
OK responds with an ok response. Intended for serverless handlers.
Types ¶
type CacheControl ¶
CacheControl specifies Cache-Control header options.
func (CacheControl) String ¶
func (c CacheControl) String() string
type Error ¶
Error is an error with an associated HTTP status code.
func BadRequest ¶
BadRequest builds a 400 error with StatusBadRequest.
func InternalServerError ¶
InternalServerError builds a 500 error.
func MethodNotAllowed ¶
MethodNotAllowed builds a 405 error.
func StatusError ¶
StatusError builds an error for the given status code, with the standard status text.
type ErrorHandler ¶
ErrorHandler wraps a HandlerFunc with an error handling layer.
func (ErrorHandler) ServeHTTP ¶
func (h ErrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type Handler ¶
type Handler interface {
HandleRequest(w http.ResponseWriter, r *http.Request) error
}
Handler handles a HTTP request and returns a possible error.
type HandlerFunc ¶
type HandlerFunc func(w http.ResponseWriter, r *http.Request) error
HandlerFunc adapts a function to the Handler interface.
func (HandlerFunc) HandleRequest ¶
func (h HandlerFunc) HandleRequest(w http.ResponseWriter, r *http.Request) error
HandleRequest calls h.
type JSONDecoder ¶
type JSONDecoder struct {
MaxRequestSize int64
}
func (*JSONDecoder) DecodeRequest ¶
func (j *JSONDecoder) DecodeRequest(w http.ResponseWriter, r *http.Request, v interface{}) error
type JSONEncoder ¶
type JSONEncoder struct {
Debug bool
}
func (*JSONEncoder) EncodeResponse ¶
func (j *JSONEncoder) EncodeResponse(w http.ResponseWriter, v interface{}) error
type Static ¶
type Static struct {
// contains filtered or unexported fields
}
Static serves static content from a filesystem. Filenames are extended with a content hash for cache busting.
func (*Static) HandleRequest ¶
HandleRequest serves a request.
func (*Static) SetCacheControl ¶
func (s *Static) SetCacheControl(cc CacheControl)
SetCacheControl configures cache control headers.