use

package
v1.46.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Access

func Access(l *slog.Logger) func(http.Handler) http.Handler

func CORS

func CORS(opts cors.Options) func(http.Handler) http.Handler

func Logger

func Logger(root *slog.Logger) func(http.Handler) http.Handler

func TraceId

func TraceId() func(http.Handler) http.Handler

func UserConfig

func UserConfig(keys jwtutil.KeySource, authnNS string) func(http.Handler) http.Handler

UserConfig builds a middleware that validates the incoming bearer token. keys resolves the RSA public key a token was signed with; in production this is a jwtutil.JWKSKeySource pointed at authn's /.well-known/jwks.json, which fetches and caches the key set so rotating authn's keypair needs no redeploy here.

Types

type Chain

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

Chain acts as a list of http.Handler constructors. Chain is effectively immutable: once created, it will always hold the same set of constructors in the same order.

func NewChain

func NewChain(constructors ...func(http.Handler) http.Handler) Chain

NewChain creates a new chain, memorizing the given list of middleware constructors. NewChain serves no other function, constructors are only called upon a call to Then().

func (Chain) Append

func (c Chain) Append(constructors ...func(http.Handler) http.Handler) Chain

Append extends a chain, adding the specified constructors as the last ones in the request flow.

Append returns a new chain, leaving the original one untouched.

stdChain := handlers.NewChain(m1, m2)
extChain := stdChain.Append(m3, m4)
// requests in stdChain go m1 -> m2
// requests in extChain go m1 -> m2 -> m3 -> m4

func (Chain) Extend

func (c Chain) Extend(chain Chain) Chain

Extend extends a chain by adding the specified chain as the last one in the request flow.

func (Chain) Then

func (c Chain) Then(h http.Handler) http.Handler

Then chains the middleware and returns the final http.Handler.

NewChain(m1, m2, m3).Then(h)

is equivalent to:

m1(m2(m3(h)))

When the request comes in, it will be passed to m1, then m2, then m3 and finally, the given handler (assuming every middleware calls the following one).

A chain can be safely reused by calling Then() several times.

stdStack := handlers.Newchain(ratelimitHandler, csrfHandler)
indexPipe = stdStack.Then(indexHandler)
authPipe = stdStack.Then(authHandler)

Note that constructors are called on every call to Then() and thus several instances of the same middleware will be created when a chain is reused in this way. For proper middleware, this should cause no problems.

Then() treats nil as http.DefaultServeMux.

func (Chain) ThenFunc

func (c Chain) ThenFunc(fn http.HandlerFunc) http.Handler

ThenFunc works identically to Then, but takes a HandlerFunc instead of a Handler.

The following two statements are equivalent:

c.Then(http.HandlerFunc(fn))
c.ThenFunc(fn)

ThenFunc provides all the guarantees of Then.

Directories

Path Synopsis
cors package is net/http handler to handle CORS related requests as defined by http://www.w3.org/TR/cors/
cors package is net/http handler to handle CORS related requests as defined by http://www.w3.org/TR/cors/

Jump to

Keyboard shortcuts

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