Documentation ¶
Index ¶
Constants ¶
const HeaderRequestID = "X-Request-Id"
HeaderRequestID is the default name of the header to extract request ids from.
Variables ¶
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.
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 ¶
HandleError returns a new Error middleware that uses f as the ErrorHandler.
func (*Error) ServeHTTPContext ¶
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 (*Logger) ServeHTTPContext ¶
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 (*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 (*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.