apimiddleware

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2025 License: MPL-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MiddlewareFunc

type MiddlewareFunc func(next http.Handler) http.Handler

MiddlewareFunc allows a simple middleware to be defined as only a function.

func (MiddlewareFunc) Middleware

func (f MiddlewareFunc) Middleware(next http.Handler) http.Handler

Middleware allows MiddlewareFunc to implement middlewareInterface.

type MiddlewareStack

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

MiddlewareStack builds a stack of middleware that a request will be dispatched through before sending it to the underlying handler. Middlewares are added to it before getting a handler with a call to Mount on another handler like a ServeMux. used like:

middlewares := &MiddlewareStack
middlewares.Use(middleware1)
middlewares.Use(middleware2)
...
handler := middlewares.Mount(mux)

Besides some slight syntactic nicety, the entire reason this type exists is because it will mount middlewares in a more human-friendly/intuitive order. When mounting middlewares (not using MiddlewareStack) like:

handler := mux
handler = middleware1.Wrapper(handler)
handler = middleware2.Wrapper(handler)
...

One must be very careful because the middlewares will be "backwards" according to the list in that when a request enters the stack, the middleware that was mounted first will be called _last_ because it's nested the deepest down.

MiddlewareStack fixes this problem by enabling any number of middlewares to be specified, and then mounting them in inverted order when Mount is called.

func NewMiddlewareStack

func NewMiddlewareStack(middlewares ...middlewareInterface) *MiddlewareStack

NewMiddlewareStack is a helper that can act as a shortcut to initialize a middleware stack by passing a series of middlewares as variadic args.

func (*MiddlewareStack) Mount

func (s *MiddlewareStack) Mount(handler http.Handler) http.Handler

func (*MiddlewareStack) Use

func (s *MiddlewareStack) Use(middleware middlewareInterface)

Jump to

Keyboard shortcuts

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