middleware

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2025 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddContextValue added in v0.6.0

func AddContextValue(key, value any, next http.Handler) http.Handler

func NoCache added in v0.6.0

func NoCache(next http.Handler) http.Handler

NoCache adds middleware that sets headers to prevent caching of the response, using response.NoCache.

func RealIP added in v0.6.0

func RealIP(next http.Handler) http.Handler

RealIP is a middleware that sets a http.Request's RemoteAddr to the results of parsing either the X-Forwarded-For header or the X-Real-IP header (in that order).

This middleware should be inserted fairly early in the middleware stack to ensure that subsequent layers (e.g., request loggers) which examine the RemoteAddr will see the intended value.

You should only use this middleware if you can trust the headers passed to you (in particular, the two headers this middleware uses), for example because you have placed a reverse proxy like HAProxy or nginx in front of the server. If your reverse proxies are configured to pass along arbitrary header values from the client, or if you use this middleware without a reverse proxy, malicious clients will be able to make you very sad (or, depending on how you're using RemoteAddr, vulnerable to an attack of some sort).

This function is based on the RealIP middleware from the Goji web framework.

func RedirectHTTPS added in v0.6.0

func RedirectHTTPS(next http.Handler) http.Handler

RedirectHTTPS adds middleware that redirects any http request to its https equivalent url, using http.Redirect. It uses the redirect code http.StatusMovedPermanently for any GET request and http.StatusTemporaryRedirect for any other method.

func RemoveTrailingSlash

func RemoveTrailingSlash(next http.Handler) http.Handler

RemoveTrailingSlash adds middleware that redirects any request which has a trailing slash to the equivalent path without trailing slash, using http.Redirect. It uses the redirect code http.StatusMovedPermanently for any GET request and http.StatusTemporaryRedirect for any other method.

func Wrap

func Wrap(h http.Handler, wrap ...Wrapper) http.Handler

Wrap http.Handler h with additional logic via the provided [Wrapper]s.

Types

type Middleware

type Middleware []Wrapper

Middleware consists of multiple [Wrapper]s which can be wrapped around a http.Handler using Wrap.

func (Middleware) Wrap

func (m Middleware) Wrap(next http.Handler) http.Handler

type Wrapper added in v0.6.0

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

Wrapper wraps the http.Handler next with additional logic.

Jump to

Keyboard shortcuts

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