abcmiddleware

package
v5.0.17 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2023 License: BSD-3-Clause, BSD-3-Clause Imports: 13 Imported by: 0

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 (
	// CTXKeyLogger is the key under which the request scoped logger is placed
	CTXKeyLogger ctxKey = iota
)

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 Logger

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

Logger 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 LoggerCTX

func LoggerCTX(ctx context.Context) *zap.Logger

LoggerCTX retrieves a logger from a context.

func RequestIDHeader

func RequestIDHeader(next http.Handler) http.Handler

RequestIDHeader sets the X-Request-ID header to the chi request id This must be used after the chi request id middleware.

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
	// Optional override layout string
	ErrLayout string
	// 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, errorLayout, 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 ErrorManager

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

ErrorManager helps manage errors at the application level

func NewErrorManager

func NewErrorManager(render abcrender.Renderer, errorLayout string) *ErrorManager

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

func (*ErrorManager) Add

func (m *ErrorManager) Add(e ErrorContainer)

Add a new ErrorContainer to the error manager

func (*ErrorManager) Errors

func (m *ErrorManager) Errors(ctrl AppHandler) http.HandlerFunc

Errors is a middleware to handle controller errors and error page rendering. The benefit of using this middleware opposed to logging and rendering errors directly in your controller is that it's all centralized to one location which simplifies adding notifiers (like slack and email). It also reduces a lot of controller boilerplate.

func (*ErrorManager) Remove

func (m *ErrorManager) Remove(e ErrorContainer)

Remove a ErrorContainer from the error manager

type MW

type MW interface {
	Wrap(http.Handler) http.Handler
}

MW is an interface defining middleware wrapping

func ZapLog

func ZapLog(logger *zap.Logger) MW

ZapLog returns a logging middleware that outputs details about a request

func ZapRecover

func ZapRecover(fallback *zap.Logger, errorHandler http.HandlerFunc) MW

ZapRecover will attempt to log a panic, as well as produce a reasonable error for the client by calling the passed in errorHandler function.

It uses the zap logger and attempts to look up a request-scoped logger created with this package before using the passed in logger.

The zap logger that's used here should be careful to enable stacktrace logging for any levels that they require it for.

func ZapRequestIDLogger

func ZapRequestIDLogger(logger *zap.Logger) MW

ZapRequestIDLogger returns a request id logger middleware. This only works if chi has inserted a request id into the stack first.

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