abcmiddleware

package
v3.0.4+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2018 License: BSD-3-Clause, BSD-3-Clause Imports: 10 Imported by: 2

README

ABCMiddleware

License GoDoc Go Report Card

Available Middleware

  • Zap - Zap middleware handles web request logging using Zap
  • Recover - Recover middleware recovers panics that occur and gracefully logs their error

See GoDoc for API usage.

Documentation

Index

Constants

View Source
const CtxLoggerKey = "request_id_logger"

CtxLoggerKey is the http.Request Context lookup key for the request ID logger

Variables

View Source
var (
	ErrUnauthorized = errors.New("not authorized")
	ErrForbidden    = errors.New("access is forbidden")
)

These are the errors that can be used in your controllers and matched against in the Errors middleware to perform special actions.

Functions

func Log

func Log(r *http.Request) *zap.Logger

Log returns the Request ID scoped logger from the request Context and panics if it cannot be found. This function is only ever used by your controllers if your app uses the RequestID middlewares, otherwise you should use the controller's receiver logger directly.

func NewErrorManager

func NewErrorManager(render abcrender.Renderer) *errorManager

NewErrorManager creates an error manager that can be used to create an error handler to wrap your controllers with

Types

type AppHandler

type AppHandler func(w http.ResponseWriter, r *http.Request) error

AppHandler is the function signature for controllers that return errors.

type ErrorContainer

type ErrorContainer struct {
	// The error that will be returned by the controller
	Err error
	// The template file top render (e.g "errors/500")
	Template string
	// HTTP status code to respond with
	Code int
	// A custom handler to perform additional operations on this error
	Handler ErrorHandler
}

ErrorContainer holds all of the relevant variables for a users custom error

func NewError

func NewError(err error, code int, template string, handler ErrorHandler) ErrorContainer

NewError creates a new ErrorContainer that can be added to an errorManager. If you provide a handler here (instead of nil) then the Errors middleware will use your handler opposed to taking the default route of logging and rendering. You must handle logging and rendering yourself.

type ErrorHandler

type ErrorHandler func(w http.ResponseWriter, r *http.Request, e ErrorContainer, render abcrender.Renderer) error

ErrorHandler is the function signature for user supplied error handlers.

type Middleware

type Middleware struct {
	// Log is used for logging in your middleware and to
	// create a derived logger that includes the request ID.
	Log *zap.Logger
}

Middleware exposes useful variables to every abcmiddleware handler

func (Middleware) Recover

func (m Middleware) Recover(next http.Handler) http.Handler

Recover middleware recovers panics that occur and gracefully logs their error

func (Middleware) RequestIDLogger

func (m Middleware) RequestIDLogger(next http.Handler) http.Handler

RequestIDLogger middleware creates a derived logger to include logging of the Request ID, and inserts it into the context object

func (Middleware) Zap

func (m Middleware) Zap(next http.Handler) http.Handler

Zap middleware handles web request logging using Zap

type MiddlewareFunc

type MiddlewareFunc func(http.Handler) http.Handler

MiddlewareFunc is the function signature for Chi's Use() middleware

Jump to

Keyboard shortcuts

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