middleware

package
v0.0.0-...-707188c Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RequestIDHeader is the name of the HTTP Header which contains the request id.
	// Exported so that it can be changed by developers
	RequestIDHeader = "X-Request-ID"
	// RequestIDContextKey is the key used to define a unique
	// identifier within echo context.
	RequestIDContextKey = "request_id"
)

Variables

This section is empty.

Functions

func CORS

func CORS(next webkit.Handler) webkit.Handler

CORS returns a Cross-Origin Resource Sharing (CORS) middleware. See also MDN: Cross-Origin Resource Sharing (CORS).

Security: Poorly configured CORS can compromise security because it allows relaxation of the browser's Same-Origin policy. See Exploiting CORS misconfigurations for Bitcoins and bounties and Portswigger: Cross-origin resource sharing (CORS) for more details.

func Gzip

func Gzip(next webkit.Handler) webkit.Handler

Gzip returns a middleware that compresses HTTP responses using gzip compression. It wraps the provided handler, adding gzip compression to responses based on the specified configuration.

func Logger

func Logger(next webkit.Handler) webkit.Handler

Logger is a middleware that logs the start and end of each request, along with some useful data about what was requested, what the response status was, and how long it took to return. When standard output is a TTY, Logger will print in color, otherwise it will print in black and white. Logger prints a request ID if one is provided.

IMPORTANT NOTE: Logger should go before any other middleware that may change the response, such as middleware.Recover. Example:

app := webkit.New()
app.Plug(middleware.Logger)        // <--<< Logger should come before Recover
app.Plug(middleware.Recover)
r.Get("/", handler)

func NonWWWRedirect

func NonWWWRedirect(next webkit.Handler) webkit.Handler

NonWWWRedirect redirects all requests to the non-www subdomain of the current host. Redirects are performed using a 301 status code (Permanent Redirect) using HTTPS.

For example, a request to "https://www.example.com" will be redirected to "https://example.com".

func Recover

func Recover(next webkit.Handler) webkit.Handler

Recover is a middleware that recovers from panics, logs the panic (and a backtrace), and returns a HTTP 500 (Internal Server Error) status if possible. The error is also reported to Sentry.

This middleware should be plugged in first to ensure that it catches any panics that occur in the request-response cycle.

func RequestID

func RequestID(next webkit.Handler) webkit.Handler

RequestID assigns a unique identifier to the contact under RequestIDContextKey. The ID is also sent back in the payload to the calling client.

func TrailingSlashRedirect

func TrailingSlashRedirect(next webkit.Handler) webkit.Handler

TrailingSlashRedirect is a middleware that will match request paths with a trailing slash and redirect to the same path, less the trailing slash.

NOTE: RedirectSlashes middleware is *incompatible* with http.FileServer.

func WWWRedirect

func WWWRedirect(next webkit.Handler) webkit.Handler

WWWRedirect redirects all requests to the www subdomain of the current host. Redirects are performed using a 301 status code (Permanent Redirect) using HTTPS.

For example, a request to "https://example.com" will be redirected to "https://www.example.com".

Types

This section is empty.

Jump to

Keyboard shortcuts

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