Documentation ¶
Overview ¶
mutableware is a mutable middleware package. You can add, remove, and swap out middleware handlers.
Index ¶
- Variables
- type AddOption
- type CurriedHandlerFunc
- type Handler
- type HandlerContainer
- func (hc *HandlerContainer[Request, Response]) Add(handler Handler[Request, Response], options ...AddOption) HandlerID
- func (hc *HandlerContainer[Request, Response]) AddAnonymousHandler(handlerFn HandlerFunc[Request, Response], options ...AddOption) HandlerID
- func (hc *HandlerContainer[Request, Response]) Handle(ctx context.Context, request Request) (Response, error)
- func (hc *HandlerContainer[Request, Response]) Remove(id HandlerID)
- type HandlerFunc
- type HandlerID
- type HandlerInfo
Constants ¶
This section is empty.
Variables ¶
var ErrHandle = errors.New("handleError")
ErrHandle is returned when one or more handlers return an error in their Handle(...) calls.
Functions ¶
This section is empty.
Types ¶
type AddOption ¶
type AddOption func(*builtAddOptions)
AddOption is an option for the Add(...) function.
func AddOptionLast ¶
func AddOptionLast() AddOption
AddOptionLast inserts the handler so it's executed last instead of first.
func AddOptionName ¶
AddOptionName attaches a name to the handler to aid in debugging. This will appear in wrapped errors and is available through the passed-in context.
func AddOptionSwap ¶
AddOptionSwap removes the target handler and inserts this one in its place. If the target handler doesn't exist, normal handler insertion occurs.
type CurriedHandlerFunc ¶
type Handler ¶
type Handler[Request any, Response any] interface { // Handle runs the handler for the request. Handle(ctx context.Context, request Request, next CurriedHandlerFunc[Request, Response]) (Response, error) }
Handler processes requests.
type HandlerContainer ¶
type HandlerContainer[Request any, Response any] struct { // contains filtered or unexported fields }
HandlerContainer is an ordered collection of Handlers of the same type. When a Request is sent to a HandlerContainer, Handlers are invoked in in the reverse order that they were added (the oldest Handler is executed last).
Handlers can be removed after being added. This is a distinguishing feature of this package versus traditional middleware packages.
func NewHandlerContainer ¶
func NewHandlerContainer[Request any, Response any]() *HandlerContainer[Request, Response]
NewHandlerContainer creates a new container for Handlers of the same type.
func (*HandlerContainer[Request, Response]) Add ¶
func (hc *HandlerContainer[Request, Response]) Add(handler Handler[Request, Response], options ...AddOption) HandlerID
Add a new handler to the container. Newer handlers are invoked first. Retain the returned HandlerID if you need to Remove() this handler later.
func (*HandlerContainer[Request, Response]) AddAnonymousHandler ¶ added in v0.2.0
func (hc *HandlerContainer[Request, Response]) AddAnonymousHandler(handlerFn HandlerFunc[Request, Response], options ...AddOption) HandlerID
Add a new handler to the container. Newer handlers are invoked first. Retain the returned HandlerID if you need to Remove() this handler later.
func (*HandlerContainer[Request, Response]) Handle ¶
func (hc *HandlerContainer[Request, Response]) Handle(ctx context.Context, request Request) (Response, error)
Handle runs the Handle function of the contained handlers. Handlers that were added latest are executed first.
func (*HandlerContainer[Request, Response]) Remove ¶
func (hc *HandlerContainer[Request, Response]) Remove(id HandlerID)
Remove a handler that was previously added.
type HandlerFunc ¶
type HandlerFunc[Request any, Response any] func(ctx context.Context, request Request, next CurriedHandlerFunc[Request, Response]) (Response, error)
func (HandlerFunc[Request, Response]) Handler ¶ added in v0.2.0
func (hf HandlerFunc[Request, Response]) Handler() Handler[Request, Response]
type HandlerID ¶
type HandlerID uint64
HandlerID identifies a handler. Use this to remove a handler from a container.
type HandlerInfo ¶
HandlerInfo contains metadata for a Handler.
func GetHandlerInfoFromContext ¶
func GetHandlerInfoFromContext(ctx context.Context) []HandlerInfo
GetHandlerInfoFromContext returns the current stack of handlers for a request. The latest handler to be invoked will be last in the slice.
func (HandlerInfo) String ¶
func (h HandlerInfo) String() string