ctxmux

package module
v0.0.0-...-f2d28bd Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2015 License: BSD-3-Clause Imports: 3 Imported by: 0

README

ctxmux Build Status

Package ctxmux provides a context.Context enabled mux using httprouter: https://godoc.org/github.com/daaku/ctxmux

Documentation

Overview

Package ctxmux provides an opinionated mux. It builds on the context library and combines it with the httprouter library known for it's performance. The equivalent of the ServeHTTP in ctxmux is:

ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request) error

It provides a hook to control context creation when a request arrives. Additionally an error can be returned which is passed thru to the error handler. The error handler is responsible for sending a response and possibly logging it as necessary. Similarly panics are also handled and passed to the panic handler.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextParams

func ContextParams(ctx context.Context) httprouter.Params

ContextParams extracts out the params from the context if possible.

func WithParams

func WithParams(ctx context.Context, p httprouter.Params) context.Context

WithParams returns a new context.Context instance with the params included.

Types

type Handler

type Handler func(ctx context.Context, w http.ResponseWriter, r *http.Request) error

Handler is an augmented http.Handler.

func HTTPHandler

func HTTPHandler(h http.Handler) Handler

HTTPHandler calls the underlying http.Handler.

func HTTPHandlerFunc

func HTTPHandlerFunc(h http.HandlerFunc) Handler

HTTPHandlerFunc calls the underlying http.HandlerFunc.

type Mux

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

Mux provides shared context initialization and error handling.

func New

func New(options ...MuxOption) (*Mux, error)

New creates a new Mux and configures it with the given options.

func (*Mux) DELETE

func (m *Mux) DELETE(path string, h Handler)

DELETE methods at path.

func (*Mux) GET

func (m *Mux) GET(path string, h Handler)

GET methods at path.

func (*Mux) HEAD

func (m *Mux) HEAD(path string, h Handler)

HEAD methods at path.

func (*Mux) Handler

func (m *Mux) Handler(method, path string, h Handler)

Handler by method and path.

func (*Mux) PATCH

func (m *Mux) PATCH(path string, h Handler)

PATCH methods at path.

func (*Mux) POST

func (m *Mux) POST(path string, h Handler)

POST methods at path.

func (*Mux) PUT

func (m *Mux) PUT(path string, h Handler)

PUT methods at path.

func (*Mux) ServeHTTP

func (m *Mux) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP allows Mux to be used as a http.Handler.

type MuxOption

type MuxOption func(*Mux) error

MuxOption are used to set various mux options.

func MuxContextMaker

func MuxContextMaker(f func(*http.Request) (context.Context, error)) MuxOption

MuxContextMaker sets a function to create the context for each request. The default context is context.Background(). If the assigned function returns an error, it will be passed to the error handler.

func MuxErrorHandler

func MuxErrorHandler(h func(context.Context, http.ResponseWriter, *http.Request, error)) MuxOption

MuxErrorHandler configures a function which is invoked for errors returned by a Handler. If one isn't set, the default behaviour is to log it and send a static error message of "internal server error".

func MuxNotFoundHandler

func MuxNotFoundHandler(h Handler) MuxOption

MuxNotFoundHandler configures a Handler that is invoked for requests where one isn't found.

func MuxPanicHandler

func MuxPanicHandler(h func(context.Context, http.ResponseWriter, *http.Request, interface{})) MuxOption

MuxPanicHandler configures a function which is invoked for panics raised while serving a request. If one is not configured, the default behavior is what the net/http package does; which is to print a trace and ignore it.

func MuxRedirectTrailingSlash

func MuxRedirectTrailingSlash() MuxOption

MuxRedirectTrailingSlash configures the Mux to automatically handling missing or extraneous trailing slashes by redirecting.

Jump to

Keyboard shortcuts

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