handlerutils

package
v0.0.0-...-2d572df Latest Latest
Warning

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

Go to latest
Published: May 28, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentTypeJSON      = "application/json; charset=UTF-8"
	ContentTypePlaintext = "text/plain"
	ContentTypeCSV       = "text/csv"
)

Defines some common ContentType strings

Variables

This section is empty.

Functions

func ContentType

func ContentType(w http.ResponseWriter, contentType string)

ContentType is a convience function to set the Content-Type header

Types

type Handler

type Handler struct {
	H func(w http.ResponseWriter, r *http.Request) error
}

Handler extends the http.Handler interface and allows handlers to return and error

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP satisfies the http.Handler interface and wraps handlers to deal with a returned error.

type HandlerError

type HandlerError struct {
	Err    error
	Status int
}

HandlerError satisfies the error interface and allows for http handlers to set a specific HTTP Status Code

func (*HandlerError) Error

func (h *HandlerError) Error() string

Error satisfies the error interface, returning the wrapped error

func (*HandlerError) StatusCode

func (h *HandlerError) StatusCode() int

StatusCode returns the configured status code

type RequestParams

type RequestParams struct {
	// contains filtered or unexported fields
}

RequestParams represents the query and path parameters of a request

func Params

func Params(r *http.Request) *RequestParams

Params is a helper function that combines URL and query parameters into a single map

func ParamsFromMap

func ParamsFromMap(values map[string][]string) *RequestParams

ParamsFromMap builds a RequestParams from the given map

func (*RequestParams) Get

func (p *RequestParams) Get(k string) (string, bool)

Get returns the requested param, if available

func (*RequestParams) Int

func (p *RequestParams) Int(k string) (int, bool)

Int returns the given parameter casted to int if possible

func (*RequestParams) MaybeInt

func (p *RequestParams) MaybeInt(k string, fallback int) int

MaybeInt returns the given parameter casted to int, or the given fallback if not set or not castable

func (*RequestParams) Require

func (p *RequestParams) Require(required ...string) error

Require ensures all passed keys are in the RequestParams

Jump to

Keyboard shortcuts

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