middleware

package
v0.0.0-...-cec8604 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// CORSDefault is the default CORS config.
	CORSDefault = CORSConfig{
		AllowOrigins:     []string{"*"},
		AllowMethods:     []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
		AllowHeaders:     []string{},
		AllowCredentials: false,
		ExposeHeaders:    []string{},
	}
)

Functions

func CORS

func CORS() func(ctx mojito.Context, next func() error) error

CORS provides a simple middleware implementation that will handle CORS requests using the default config

func CORSWithConfig

func CORSWithConfig(config CORSConfig) func(ctx mojito.Context, next func() error) error

CORSWithConfig provides a simple middleware implementation that will handle CORS requests

func Compression

func Compression(ctx mojito.Context, next func() error) (err error)

Compression compresses the http response if supported by the client.

func Logging

func Logging(ctx mojito.Context, next func() error) (err error)

Logging provides a simple middleware implementation that will log every request handled by mojito

func RequestLogger

func RequestLogger(ctx mojito.Context, next func() error) (err error)

RequestLogger provides a simple middleware implementation that will log every request handled by mojito Deprecated: use middleware.Logging instead

Types

type CORSConfig

type CORSConfig struct {
	// The Access-Control-Allow-Origin response header indicates whether the response can be
	// shared with requesting code from the given origin.
	//
	// Optional. Default value []string{"*"}.
	//
	// See also: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
	AllowOrigins []string

	// The Access-Control-Allow-Methods response header specifies one or more methods allowed when
	// accessing a resource in response to a preflight request.
	//
	// Optional. Default value "GET,POST,HEAD,PUT,DELETE,PATCH"
	//
	// See also: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods
	AllowMethods []string

	// The Access-Control-Allow-Headers response header is used in response to a preflight request which includes
	// the Access-Control-Request-Headers to indicate which HTTP headers can be used during the actual request.
	//
	// Optional. Default value []string{}.
	//
	// See also: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers
	AllowHeaders []string

	// The Access-Control-Allow-Credentials response header tells browsers whether to expose the response
	// to the frontend JavaScript code when the request's credentials mode (Request.credentials) is include.
	//
	// Optional. Default value false.
	//
	// See also: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials
	AllowCredentials bool

	// The Access-Control-Expose-Headers response header allows a server to indicate which response headers
	// should be made available to scripts running in the browser, in response to a cross-origin request.
	//
	// Optional. Default value []string{}.
	//
	// See also: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers
	ExposeHeaders []string `yaml:"expose_headers"`

	// The Access-Control-Max-Age response header indicates how long the results of a preflight request
	// (that is the information contained in the Access-Control-Allow-Methods and Access-Control-Allow-Headers headers)
	// can be cached.
	//
	// Optional. Default value 0.
	//
	// See also: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Max-Age
	MaxAge int
}

Jump to

Keyboard shortcuts

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