httputil

package
v0.0.0-...-c9cfaf6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2020 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Overview

Package httputil provides utilities for implementing web frontends and APIs with the Go standard library.

Index

Constants

This section is empty.

Variables

View Source
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.

View Source
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 DecodeJSON(r io.Reader, v interface{}) error

func ExpectStatus

func ExpectStatus(code int, allow ...int) error

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

type CacheControl struct {
	MaxAge       time.Duration
	SharedMaxAge time.Duration
	Directives   []string
}

CacheControl specifies Cache-Control header options.

func (CacheControl) String

func (c CacheControl) String() string

type Error

type Error struct {
	Code int
	Err  error
}

Error is an error with an associated HTTP status code.

func BadRequest

func BadRequest(err error) Error

BadRequest builds a 400 error with StatusBadRequest.

func InternalServerError

func InternalServerError(err error) Error

InternalServerError builds a 500 error.

func MethodNotAllowed

func MethodNotAllowed(method string) Error

MethodNotAllowed builds a 405 error.

func NotFound

func NotFound() Error

NotFound builds a 404 error.

func StatusError

func StatusError(code int) Error

StatusError builds an error for the given status code, with the standard status text.

func (Error) Error

func (e Error) Error() string

func (Error) Status

func (e Error) Status() int

Status returns the associated HTTP status code.

type ErrorHandler

type ErrorHandler struct {
	Handler Handler
	Log     *zap.Logger
}

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 NewStatic

func NewStatic(filesys fs.Readable) *Static

NewStatic builds a static file HTTP handler.

func (*Static) HandleRequest

func (s *Static) HandleRequest(w http.ResponseWriter, r *http.Request) error

HandleRequest serves a request.

func (*Static) Path

func (s *Static) Path(ctx context.Context, name string) (string, error)

Path returns the versioned path for name, intended for cache busting.

func (*Static) SetCacheControl

func (s *Static) SetCacheControl(cc CacheControl)

SetCacheControl configures cache control headers.

func (*Static) SetLogger

func (s *Static) SetLogger(l *zap.Logger)

SetLogger configures logging output from the static handler.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL