middleware

package
v1.31.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chain

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

Chain is an ordered collection of named middleware. The chain allows locating middleware by a name. It is advised to use a unique name for each middleware.

func (*Chain) Append

func (ch *Chain) Append(name string, mw Middleware)

Append adds a middleware to the end of the chain.

func (*Chain) Clear

func (ch *Chain) Clear()

Clear removes all middleware from the chain.

func (*Chain) Delete

func (ch *Chain) Delete(name string) (ok bool)

Delete removes the first occurrence of the named middleware, if found.

func (*Chain) Exists

func (ch *Chain) Exists(name string) (ok bool)

Exists indicates if a middleware with the given name exists in the chain.

func (*Chain) Handlers

func (ch *Chain) Handlers() (handlers []Middleware)

Handlers returns the ordered list of middleware handlers.

func (*Chain) InsertAfter

func (ch *Chain) InsertAfter(afterName string, name string, mw Middleware) (ok bool)

InsertAfter inserts a middleware after the last occurrence of the named middleware, if found.

func (*Chain) InsertBefore

func (ch *Chain) InsertBefore(beforeName string, name string, mw Middleware) (ok bool)

InsertBefore inserts a middleware before the first occurrence of the named middleware, if found.

func (*Chain) Prepend

func (ch *Chain) Prepend(name string, mw Middleware)

Prepend adds a middleware to the beginning of the chain.

func (*Chain) Replace

func (ch *Chain) Replace(name string, mw Middleware) (ok bool)

Replace replaces the first occurrence of the named middleware, if found.

func (*Chain) String

func (ch *Chain) String() string

String returns the names of the middleware in the chain, in order of their appearance.

type Middleware

type Middleware func(next connector.HTTPHandler) connector.HTTPHandler

Middleware returns a function that can pre or post process the request or response. The processor should generally call the next function in the chain.

func Authorization

func Authorization(exchange func(ctx context.Context, bearerToken string) (accessToken string, err error)) Middleware

Authorization returns a middleware that looks for a token in the "Authorization: Bearer" header or the "Authorization" cookie. If the external bearer token is validated with its issuer, the exchange callback returns a signed internal access token JWT to set as the actor.

func BlockedPaths

func BlockedPaths(isBlocked func(path string) bool) Middleware

BlockedPaths returns a middleware that returns a 404 error for requests with paths matching the predicate. The path passed to the matcher is the full path of the URL, without query arguments.

func CacheControl

func CacheControl(defaultValue string) Middleware

CacheControl returns a middleware that sets the Cache-Control header if not otherwise specified.

func CharsetUTF8

func CharsetUTF8() Middleware

CharsetUTF8 returns a middleware that augments the Content-Type header of text/* and application/json with the UTF-8 charset.

func Compress

func Compress() Middleware

Compress returns a middleware that compresses textual responses using brotli, gzip or deflate.

func Cors

func Cors(allowedOrigin func(r *http.Request, origin string) string) Middleware

Cors returns a middleware that responds to the CORS origin OPTION request and blocks requests from disallowed origins. allowedOrigin returns the value to set in Access-Control-Allow-Origin for the given request and Origin header. An empty return value rejects the request with 403.

func DefaultFavIcon

func DefaultFavIcon() Middleware

DefaultFavIcon returns a middleware that responds to /favicon.ico, if the app does not.

func ErrorPageRedirect

func ErrorPageRedirect(statusCode int, errorPagePath string) Middleware

ErrorPageRedirect returns a middleware that redirects HTTP errors to an error page. Only requests coming from a browser as indicated by Sec-Fetch-Mode:navigate and Sec-Fetch-Dest:document are redirected. The URL of the original page is passed in the "src" parameter to the error page.

func ErrorPrinter

func ErrorPrinter(redact func() bool) Middleware

ErrorPrinter returns a middleware that outputs any error to the response body. It should typically be the first middleware in the chain in case any of the other middleware fail. Error details and stack trace can be optionally redacted.

The printer outputs the error as a JSON object nested the property "err" of the root object, with its nested properties up-leveled as if they are properties of the error.

{
	"err": {
		"error": "message",
		"stack": [...],
		"statusCode": 500,
		"trace": "0123456789abcdef0123456789abcdef",
		"propName": "propValue"
	}
}

func Group

func Group(nested ...Middleware) Middleware

Group returns a middleware that nested middleware together and is often used in conjunction with the `OnRoute` middleware to apply a group of middleware to a specific route.

func InternalHeaders

func InternalHeaders() Middleware

InternalHeaders returns a middleware that filters internal headers from entering or exiting.

func Logger

func Logger(logger service.Logger) Middleware

Logger returns a middleware that logs the incoming requests and error responses.

func NoOp

func NoOp() Middleware

NoOp returns a middleware that delegates to the next middleware in the chain without taking any action. It can be used to mark a position in the chain.

func OnRoute

func OnRoute(applyToPath func(path string) bool, conditional Middleware) Middleware

OnRoute returns a middleware that applies the conditional middleware only for URL paths that match the predicate.

func OnRoutePrefix

func OnRoutePrefix(pathPrefix string, conditional Middleware) Middleware

OnRoutePrefix returns a middleware that applies the conditional middleware only for URL paths that start with the prefix.

httpIngress.Middleware().Append(middleware.OnRoutePrefix("/images/", middleware.CacheControl("private")))

func OnRouteRegex

func OnRouteRegex(re *regexp.Regexp, conditional Middleware) Middleware

OnRouteRegex returns a middleware that applies the conditional middleware only for URL paths that match the regexp.

func RootPath

func RootPath(rootPath string) Middleware

RootPath returns a middleware that rewrites the root path "/" with one that can be routed to such as "/root".

func SecureRedirect

func SecureRedirect(isSecurePort443 func() bool) Middleware

SecureRedirect returns a middleware that redirects requests from HTTP port :80 to HTTPS port :443, if appropriate.

func Timeout

func Timeout(budget func() time.Duration) Middleware

Timeout returns a middleware that applies a timeout to the request.

func XForwarded

func XForwarded() Middleware

XForwarded returns a middleware that sets the `X-Forwarded` headers pertaining to the request, if not already set. These headers are used by downstream microservices to compose absolute URLs when necessary.

Jump to

Keyboard shortcuts

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