middleware

package
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2023 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

CORS跨域处理中间件

errors错误处理中间件

日志记录中间件

panic错误捕获处理

接口限流中间件,根据实际需求可注册为全局|指定路由组|指定路由中间件

Index

Constants

View Source
const (
	FillInterval       = 1 * time.Second //默认填充速率
	Capacity     int64 = 200             //默认令牌桶容量
	Quantum      int64 = 10              //默认单次填充令牌数量
)

Variables

This section is empty.

Functions

func CorsAllowedOrigin

func CorsAllowedOrigin(c *gin.Context, options CorsOptions) bool

CORSAllowedOrigin CORSAllowed checks whether the current request origin is allowed cross-domain.

func CorsDefault

func CorsDefault(c *gin.Context)

CORSDefault sets CORS with default CORS options, which allows any cross-domain request.

func CorsHandle added in v1.1.0

func CorsHandle(conf CorsOptions) gin.HandlerFunc

func ErrorHandle

func ErrorHandle() gin.HandlerFunc

ErrorHandle

func LoggerHandle added in v1.1.0

func LoggerHandle(ploger *plog.Output, logPlatform string, conf interface{}) gin.HandlerFunc

LoggerHandle plog: *plog.Output 日志服务指针 logPlatform: string 平台名 conf: interface{} 日志平台配置

func PanicHandle added in v1.1.0

func PanicHandle(appName string, emailServerConfig pmailer.EmailSererConfig, receivers []string, emailTpl string) gin.HandlerFunc

PanicHandle 捕获服务允许过程中发生的panic错误 appName: string 系统服务名 emailServerConfig: mailer.EmailSererConfig 发件服务配置 receivers: []string 告警邮件收件人地址 emailTpl: string 邮件模板

func RateLimiterHandle added in v1.1.0

func RateLimiterHandle(fillInterval time.Duration, cap int64) gin.HandlerFunc

fillInterval: 令牌填充间隔 cap: 令牌桶容量

func SetCors

func SetCors(c *gin.Context, options CorsOptions)

CORS sets custom CORS options. See https://www.w3.org/TR/cors/ .

func SignHandle added in v1.1.0

func SignHandle(signExpire time.Duration, signKey string) gin.HandlerFunc

SignHandle 接口验签 signExpire: time.Duration 验签有效期 signKey: string 签名秘钥

Types

type CorsOptions

type CorsOptions struct {
	AllowDomain      []string `yaml:"allowDomain" json:"allowDomain"`           // Used for allowing requests from custom domains
	AllowOrigin      string   `yaml:"allowOrigin" json:"allowOrigin"`           // Access-Control-Allow-Origin
	AllowCredentials string   `yaml:"allowCredentials" json:"allowCredentials"` // Access-Control-Allow-Credentials
	ExposeHeaders    string   `yaml:"exposeHeaders" json:"exposeHeaders"`       // Access-Control-Expose-Headers
	MaxAge           int      `yaml:"maxAge" json:"maxAge"`                     // Access-Control-Max-Age
	AllowMethods     string   `yaml:"allowMethod" json:"allowMethod"`           // Access-Control-Allow-Methods
	AllowHeaders     string   `yaml:"allowHeaders" json:"allowHeaders"`         // Access-Control-Allow-Headers
}

服务端允许跨域请求选项

func DefaultCorsOptions

func DefaultCorsOptions(c *gin.Context) CorsOptions

DefaultCorsOptions returns the default CORS options, which allows any cross-domain request.

Jump to

Keyboard shortcuts

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