Documentation ¶
Index ¶
Constants ¶
const MimeTypeHeader = "Content-Type"
Variables ¶
var ( ErrTooManyInputArgs = errors.New("autoroute: a function can only have up to three input args") ErrTooManyOutputArgs = errors.New("autoroute: a function can only have up to two output args") )
var ( ErrNoFunction = errors.New("autoroute: not a function passed to NewHandler") ErrDecodeFailure = errors.New("autoroute: failure decoding input") )
var ( ErrAlreadyRegistered = errors.New("autoroute: route already registered") ErrInvalidMethod = errors.New("autoroute: not a valid method") )
var (
ErrBadErrorHandlerArgs = errors.New("autoroute: error handlers must have two input args")
)
Functions ¶
func DefaultErrorHandler ¶
func DefaultErrorHandler(w http.ResponseWriter, x error)
DefaultErrorHandler writes json `{"error": "errString"}`
Types ¶
type BasicAuthMiddleware ¶
type BasicAuthMiddleware struct {
// contains filtered or unexported fields
}
func NewBasicAuthMiddleware ¶
func NewBasicAuthMiddleware(user, pwd string) *BasicAuthMiddleware
type Codec ¶
type Codec interface { Mime() string ValidFn(fn reflect.Value) error HandleRequest(*CodecRequestArgs) }
A Codec implements pluggable, mime-type based serialization and deserialization for autoroute based Handlers
var JSONCodec Codec = jsonCodec{}
JSONCodec implements autoroute functionality for the mime type application/json and functions that have up to three input args and two output args. a function with three input args must look like func(context.Context, autoroute.Header, anyStructOrPointer) with two it can be func(context.Context, autoroute.Header) or func(context.Context, anyStructOrPointer) with one it can be any of func(context.Context), func(autoroute.Header), or func(anyStructOrPointer) in terms of output values, a function can return `(anyStructOrPointer, error)`, `(anyStructOrPointer)`, (error), or nothing. the JSONCodec will attempt to decode values in two ways 1. use encoding/json on the request body 2. decode the URL parameters of a GET request into the struct and it will always JSON encode the output value.
type CodecRequestArgs ¶
type CodecRequestArgs struct { ResponseWriter http.ResponseWriter Request *http.Request ErrorHandler ErrorHandler Header Header // Our nice bit of reflection stuff to work with HandlerFn reflect.Value HandlerType reflect.Type InputArgCount, OutputArgCount int MaxSizeBytes int64 }
CodecRequestArgs is passed to a Codec when it matches the mime type of a given request
type ErrorHandler ¶
type ErrorHandler func(w http.ResponseWriter, e error)
An ErrorHandler is responsible for writing an error back to the calling http client
func (ErrorHandler) Handle ¶
func (eh ErrorHandler) Handle(w http.ResponseWriter, errorValue reflect.Value)
Handle is a convienience method on ErrorHandler that allows it to call itself with a reflect.Value
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler wraps a function and uses reflection and pluggable codecs to automatically create a fast, safe http.Handler from the function
func NewHandler ¶
func NewHandler(x interface{}, opts ...HandlerOption) (*Handler, error)
NewHandler creates an http.Handler from a function that fits a codec-specified layout.
type HandlerOption ¶
type HandlerOption func(h *Handler)
func WithCodec ¶
func WithCodec(c Codec) HandlerOption
func WithErrorHandler ¶
func WithErrorHandler(errH ErrorHandler) HandlerOption
func WithMaxSizeBytes ¶
func WithMaxSizeBytes(maxSizeBytes int64) HandlerOption
func WithMiddleware ¶
func WithMiddleware(m Middleware) HandlerOption
func WithName ¶
func WithName(n string) HandlerOption
type Middleware ¶
type Middleware interface { // Before can modify an incoming request in the middleware chain Before(r *http.Request, h *Handler) error }
Middleware is used for things such as authentication / authorization controls checking specific headers of a request etc
type MiddlewareError ¶
func (MiddlewareError) Error ¶
func (mwe MiddlewareError) Error() string
type Router ¶
Router implements an autoroute aware grouping of autoroute.Handler's
func NewRouter ¶
func NewRouter(handlerOptions ...HandlerOption) (*Router, error)
type SignedHeadersMiddleware ¶
type SignedHeadersMiddleware struct {
// contains filtered or unexported fields
}
SignedHeadersMiddleware validates that all incoming headers are signed using a certain key if they're set as a header outgoing, they'll also be signed on the way out. this works great for cookies.
func NewSignedHeadersMiddleware ¶
func NewSignedHeadersMiddleware(headers []string, key string) *SignedHeadersMiddleware
func (*SignedHeadersMiddleware) Before ¶
func (shm *SignedHeadersMiddleware) Before(r *http.Request, h *Handler) error