httputil

package
v0.0.0-...-1327151 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 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

This section is empty.

Functions

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

func NewStatic

func NewStatic(filesys fs.Readable) Handler

NewStatic serves static content from the supplied filesystem.

func ProxySingleURL

func ProxySingleURL(u *url.URL) Handler

ProxySingleURL is a handler that serves the content at the given URL.

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

Jump to

Keyboard shortcuts

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