middleware

package
v5.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2023 License: Apache-2.0 Imports: 10 Imported by: 2

Documentation

Overview

Package middleware is the collection of the middlewares.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateToken

func GenerateToken(n int, charsets ...string) func() string

GenerateToken returns a token generator which will generate a n-length token string.

Types

type CORSConfig

type CORSConfig struct {
	// AllowOrigin defines a list of origins that may access the resource.
	//
	// Optional. Default: []string{"*"}.
	AllowOrigins []string

	// AllowHeaders indicates a list of request headers used in response to
	// a preflight request to indicate which HTTP headers can be used when
	// making the actual request. This is in response to a preflight request.
	//
	// Optional. Default: []string{}.
	AllowHeaders []string

	// AllowMethods indicates methods allowed when accessing the resource.
	// This is used in response to a preflight request.
	//
	// Optional. Default: []string{"HEAD", "GET", "POST", "PUT", "PATHC", "DELETE"}.
	AllowMethods []string

	// ExposeHeaders indicates a server whitelist headers that browsers are
	// allowed to access. This is in response to a preflight request.
	//
	// Optional. Default: []string{}.
	ExposeHeaders []string

	// AllowCredentials indicates whether or not the response to the request
	// can be exposed when the credentials flag is true. When used as part of
	// a response to a preflight request, this indicates whether or not the
	// actual request can be made using credentials.
	//
	// Optional. Default: false.
	AllowCredentials bool

	// MaxAge indicates how long (in seconds) the results of a preflight request
	// can be cached.
	//
	// Optional. Default: 0.
	MaxAge int
}

CORSConfig is used to configure the CORS middleware.

type GZipConfig

type GZipConfig struct {
	// Level is the compression level, range [-1, 9].
	//
	// Default: -1 (default compression level)
	Level int

	// Domains is the host domains enabling the gzip compression.
	// If empty, compress all the requests to all the host domains.
	//
	// The domain name supports the exact, prefix and suffix match. For example,
	//   Exact:  www.example.com
	//   Prefix: www.example.*
	//   Suffix: *.example.com
	//
	// Default: nil
	Domains []string
}

GZipConfig is used to configure the GZIP middleware.

type Middleware

type Middleware = ship.Middleware

Middleware is the alias of ship.Middleware.

func BodyLenLimit

func BodyLenLimit(maxBodySize int64) Middleware

BodyLenLimit is used to limit the maximum body of the request.

func CORS

func CORS(config *CORSConfig) Middleware

CORS returns a CORS middleware.

func Gzip

func Gzip(config *GZipConfig) Middleware

Gzip returns a middleware to compress the response body by GZIP.

Notice:

  1. the returned gzip middleware will always compress it, no matter whether the response body is empty or not.
  2. the gzip middleware must be the last to handle the response. If returning an error stands for the failure result, therefore, it should be handled before compressing the response body, that's, the error handler middleware must be appended after the GZip middleware.

func Logger

func Logger() Middleware

Logger returns a new logger middleware that will log the request.

func MaxRequests

func MaxRequests(max uint32) Middleware

MaxRequests returns a Middleware to allow the maximum number of the requests to max at a time.

If the number of the requests exceeds the maximum, it will return the error ship.ErrTooManyRequests.

func Recover

func Recover() Middleware

Recover returns a middleware to wrap the panic.

Jump to

Keyboard shortcuts

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