middleware

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2015 License: BSD-2-Clause Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const HeaderRequestID = "X-Request-Id"

HeaderRequestID is the default name of the header to extract request ids from.

Variables

View Source
var DefaultErrorHandler = func(err error, w http.ResponseWriter, r *http.Request) {
	http.Error(w, err.Error(), http.StatusInternalServerError)
}

DefaultErrorHandler is an error handler that will respond with the error message and a 500 status.

View Source
var DefaultGenerator = context.Background

DefaultGenerator is the default context generator. Defaults to just use context.Background().

Functions

This section is empty.

Types

type Background

type Background struct {
	// Generate will be called to generate a context.Context for the
	// request.
	Generate func() context.Context
	// contains filtered or unexported fields
}

Background is middleware that implements the http.Handler interface to inject an initial context object. Use this as the entry point from an http.Handler server.

func BackgroundContext

func BackgroundContext(h httpx.Handler) *Background

func (*Background) ServeHTTP

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

ServeHTTP implements the http.Handler interface.

func (*Background) ServeHTTPContext

func (h *Background) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r *http.Request) error

type Error

type Error struct {
	// ErrorHandler is a function that will be called when a handler returns
	// an error.
	ErrorHandler func(error, http.ResponseWriter, *http.Request)
	// contains filtered or unexported fields
}

Error is an httpx.Handler that will handle errors with an ErrorHandler.

func HandleError

func HandleError(h httpx.Handler, f func(error, http.ResponseWriter, *http.Request)) *Error

HandleError returns a new Error middleware that uses f as the ErrorHandler.

func NewError

func NewError(h httpx.Handler) *Error

func (*Error) ServeHTTPContext

func (h *Error) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r *http.Request) error

ServeHTTPContext implements the httpx.Handler interface.

type Logger

type Logger struct {
	// Device is an io.Writer to write logs to.
	Device io.Writer
	// contains filtered or unexported fields
}

Logger is middleware that will insert a logger.Logger into the context.

func NewLogger

func NewLogger(h httpx.Handler, d io.Writer) *Logger

func (*Logger) ServeHTTPContext

func (h *Logger) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r *http.Request) error

type Recovery

type Recovery struct {
	// Reporter is a Reporter that will be inserted into the context. It
	// will also be used to report panics.
	reporter.Reporter
	// contains filtered or unexported fields
}

Recovery is a middleware that will recover from panics and return the error.

func Recover

func Recover(h httpx.Handler, r reporter.Reporter) *Recovery

func (*Recovery) ServeHTTPContext

func (h *Recovery) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r *http.Request) (err error)

ServeHTTPContext implements the httpx.Handler interface. It recovers from panics and returns an error for upstream middleware to handle.

type RequestID

type RequestID struct {
	// Header is the name of the http header to extract the request id from.
	// The zero value is the value of HeaderRequestID.
	Header string
	// contains filtered or unexported fields
}

RequestID is middleware that extracts a request id from the headers and inserts it into the context.

func ExtractRequestID

func ExtractRequestID(h httpx.Handler) *RequestID

func (*RequestID) ServeHTTPContext

func (h *RequestID) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r *http.Request) error

ServeHTTPContext implements the httpx.Handler interface. It extracts a request id from the headers and inserts it into the context.

Jump to

Keyboard shortcuts

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