middleware

package
v0.18.4 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var WithEncryptID = serializer.WithEncryptID

WithEncryptID 注入 ID 加密函数。

View Source
var WithFormatTime = serializer.WithFormatTime

WithFormatTime 注入时间值格式化函数。

View Source
var WithIDField = serializer.WithIDField

WithIDField 注入 ID 字段判断函数。

View Source
var WithMaxDepth = serializer.WithMaxDepth

WithMaxDepth 注入递归处理最大深度。

View Source
var WithSkipField = serializer.WithSkipField

WithSkipField 注入字段跳过判断函数。

View Source
var WithTimeField = serializer.WithTimeField

WithTimeField 注入时间字段判断函数。

Functions

func Error added in v0.13.24

func Error(c *gin.Context, code int, message string)

Error 返回错误响应。

参数:

  • c: gin 上下文
  • code: 错误码
  • message: 错误消息

func GetRealIP added in v0.13.3

func GetRealIP(c *gin.Context, useCDN bool) (realIP string)

GetRealIP 获取真实客户端IP地址 当useCDN为true时,优先从代理头信息中获取IP;为false时,直接使用gin的ClientIP方法 参数:

  • c: gin上下文
  • useCDN: 是否使用CDN或代理,true表示使用,false表示不使用

返回值:

  • realIP: 真实客户端IP地址

func GetRealIPWithFilter added in v0.13.5

func GetRealIPWithFilter(c *gin.Context, useCDN bool, whitelist, blacklist []string) (realIP string)

GetRealIPWithFilter 获取真实客户端IP地址(带过滤功能) 参数:

  • c: gin上下文
  • useCDN: 是否使用CDN或代理,true表示使用,false表示不使用
  • whitelist: IP白名单列表,如果提供则只允许白名单内的IP
  • blacklist: IP黑名单列表,如果提供则拒绝黑名单内的IP

返回值:

  • realIP: 真实客户端IP地址

func JSON added in v0.13.24

func JSON(c *gin.Context, code int, message string, data interface{})

JSON 返回统一 JSON 响应。

参数:

  • c: gin 上下文
  • code: 响应码
  • message: 响应消息
  • data: 响应数据

func Success added in v0.13.24

func Success(c *gin.Context, data interface{}, opts ...Option)

Success 返回成功响应,并统一处理时间字段、按需加密 ID。 通过函数式选项注入时间格式化与 ID 加密策略;不注入则不处理。

参数:

  • c: gin 上下文
  • data: 响应数据
  • opts: 可选的处理策略

Types

type Base

type Base struct {
}

func (Base) Cors

func (b Base) Cors() gin.HandlerFunc

Cors 放行任意来源的跨域中间件(历史行为,向后兼容保留) 等价于 CorsWithConfig(CorsConfig{AllowAllOrigins: true, AllowCredentials: true}) 注意:该模式等价于"任意网站可携带 Cookie 跨域读写",生产环境建议改用 CorsWithConfig 配置白名单收紧 返回值:

  • gin.HandlerFunc: 注册到引擎的中间件

使用示例:

base := middleware.Base{}
r.Use(base.Cors())

func (Base) CorsWithConfig added in v0.17.11

func (b Base) CorsWithConfig(conf CorsConfig) gin.HandlerFunc

CorsWithConfig 按配置处理跨域的中间件 白名单未命中的来源不下发任何 CORS 响应头,由浏览器自行拦截(预检同样返回 204 但不带授权头), 服务端路由仍会正常执行,不在预检阶段回 403 以缩小探测面 参数:

  • conf: CORS 配置,见 CorsConfig 各字段说明

返回值:

  • gin.HandlerFunc: 注册到引擎的中间件

异常:不触发 panic 使用示例:

base := middleware.Base{}
r.Use(base.CorsWithConfig(middleware.CorsConfig{
    AllowOrigins:     []string{"https://www.example.com", "*.api.example.com"},
    AllowCredentials: true,
}))

func (Base) SetGPC

func (b Base) SetGPC(e *gin.Engine) gin.HandlerFunc

SetGPC 把 query、cookie、header 与请求体参数汇总到上下文的 "GPC" 键 请求体按 Content-Type 分派解析,读到的字节一律回灌,业务 handler 仍可重复读取原始请求体 参数:

  • e: gin 引擎,取用其 MaxMultipartMemory 作为 multipart 的内存上限

返回值:

  • gin.HandlerFunc: 注册到引擎的中间件

异常:不触发 panic;请求体解析失败只记录日志并给出空的 data,GPC 结构始终完整写入 使用示例:

base := middleware.Base{}
e := gin.Default()
e.Use(base.SetGPC(e))

func (Base) SetRealIP added in v0.13.3

func (b Base) SetRealIP(useCDN bool) gin.HandlerFunc

SetRealIP 设置真实IP到gin上下文

type CorsConfig added in v0.17.11

type CorsConfig struct {
	// AllowAllOrigins 放行任意来源并回显 Origin(Cors() 的历史行为)。
	// 开启后 AllowOrigins 白名单被忽略
	AllowAllOrigins bool

	// AllowOrigins 来源白名单,仅在 AllowAllOrigins 为 false 时生效。
	// 支持两种写法:
	//   - 精确匹配:完整 Origin 字符串,含 scheme 与端口,如 "https://a.example.com:8443"
	//   - 子域通配符:"*.example.com",匹配 example.com 及其任意子域,scheme 不限
	// 白名单为空且未开启 AllowAllOrigins 时,任何来源都不会被授予 CORS 头
	AllowOrigins []string

	// AllowCredentials 是否允许携带 Cookie 等凭证。
	// 仅在来源被授予且 Allow-Origin 不是 "*" 时下发,避免与通配符冲突
	AllowCredentials bool
}

CorsConfig 跨域中间件的配置项

type Option added in v0.13.24

type Option = serializer.Option

Option 为序列化选项函数类型的别名。

type Options added in v0.13.24

type Options = serializer.Options

Options 为序列化处理选项的别名,保持与原有 middleware 调用方式一致。

type Response added in v0.13.24

type Response struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

Response 统一响应结构。

Jump to

Keyboard shortcuts

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