middleware

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package middleware 提供 ginx 的内置中间件集合, 每个中间件独立一个文件; 各中间件均可脱离 ginx, 直接用于原生 gin。

当前提供:

  • CORS: 跨域资源共享中间件(见 cors.go)
  • RateLimit: 固定窗口限流中间件(见 ratelimit.go)

使用示例:

// 方式一: 经 ginx 配置启用, 对应配置节点非 nil 时自动注入
r := &ginx.Gin{
	CORS:      &middleware.CORS{AllowOrigins: []string{"https://example.com"}},
	RateLimit: &middleware.RateLimit{Limit: 100, Window: time.Second},
}

// 方式二: 脱离 ginx, 直接用于原生 gin
h, err := (&middleware.CORS{AllowOrigins: []string{"https://example.com"}}).Handler()
if err != nil {
	return err
}
e := gin.New()
e.Use(h)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CORS

type CORS struct {
	AllowOrigins     []string      `yaml:"allow_origins" toml:"allow_origins" json:"allow_origins" mapstructure:"allow_origins"`                 // 允许的源(协议+域名+端口), "*" 匹配全部(默认 *)
	AllowMethods     []string      `yaml:"allow_methods" toml:"allow_methods" json:"allow_methods" mapstructure:"allow_methods"`                 // 允许的方法, 默认 GET/POST/PUT/PATCH/DELETE/HEAD/OPTIONS
	AllowHeaders     []string      `yaml:"allow_headers" toml:"allow_headers" json:"allow_headers" mapstructure:"allow_headers"`                 // 允许的请求头, 默认 Origin/Content-Type/Accept/Authorization/X-Requested-With
	ExposeHeaders    []string      `yaml:"expose_headers" toml:"expose_headers" json:"expose_headers" mapstructure:"expose_headers"`             // 允许前端读取的响应头(可选)
	AllowCredentials bool          `yaml:"allow_credentials" toml:"allow_credentials" json:"allow_credentials" mapstructure:"allow_credentials"` // 允许携带凭证(Cookie), 不可与 "*" 源同时使用
	MaxAge           time.Duration `yaml:"max_age" toml:"max_age" json:"max_age" mapstructure:"max_age"`                                         // 预检结果缓存时长(<=0 时默认 12h)
}

CORS 跨域资源共享配置, 由 ginx.Gin.CORS 设置后启用内置跨域中间件; 未设置的字段自动使用默认值。也可脱离 ginx, 经 Handler 直接用于原生 gin。

func (*CORS) Handler

func (c *CORS) Handler() (gin.HandlerFunc, error)

Handler 构建跨域中间件: 未设置的字段自动填充默认值, 配置不合法时返回错误。 ginx 在 Gin.CORS 非 nil 时自动注入; 也可在原生 gin 中直接 e.Use 使用。

type RateLimit

type RateLimit struct {
	Limit  int           `yaml:"limit" toml:"limit" json:"limit" mapstructure:"limit"`     // 单个客户端每个窗口内允许的最大请求数(<=0 时默认 100)
	Window time.Duration `yaml:"window" toml:"window" json:"window" mapstructure:"window"` // 统计时间窗口(<=0 时默认 1s)

	// KeyFunc 限流键生成函数, 返回相同键的请求共享同一配额;
	// 默认取客户端 IP, 需按用户 ID / API Key 限流时注入; 仅支持代码注入
	KeyFunc func(c *gin.Context) string `yaml:"-" toml:"-" json:"-" mapstructure:"-"`
}

RateLimit 固定窗口限流配置, 由 ginx.Gin.RateLimit 设置后启用内置限流中间件; 未设置的字段自动使用默认值。也可脱离 ginx, 经 Handler 直接用于原生 gin。

func (*RateLimit) Handler

func (r *RateLimit) Handler() (gin.HandlerFunc, error)

Handler 构建限流中间件: 未设置的字段自动填充默认值。 以固定窗口统计各键的请求次数, 超限返回 429 与 Retry-After; ginx 在 Gin.RateLimit 非 nil 时自动注入, 也可在原生 gin 中直接 e.Use 使用。

Jump to

Keyboard shortcuts

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