middleware

package
v0.16.6 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 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 开放所有接口的OPTIONS方法

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 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