Documentation
¶
Index ¶
- Variables
- func Error(c *gin.Context, code int, message string)
- func GetRealIP(c *gin.Context, useCDN bool) (realIP string)
- func GetRealIPWithFilter(c *gin.Context, useCDN bool, whitelist, blacklist []string) (realIP string)
- func JSON(c *gin.Context, code int, message string, data interface{})
- func Success(c *gin.Context, data interface{}, opts ...Option)
- type Base
- type CorsConfig
- type Option
- type Options
- type Response
Constants ¶
This section is empty.
Variables ¶
var WithEncryptID = serializer.WithEncryptID
WithEncryptID 注入 ID 加密函数。
var WithFormatTime = serializer.WithFormatTime
WithFormatTime 注入时间值格式化函数。
var WithIDField = serializer.WithIDField
WithIDField 注入 ID 字段判断函数。
var WithMaxDepth = serializer.WithMaxDepth
WithMaxDepth 注入递归处理最大深度。
var WithSkipField = serializer.WithSkipField
WithSkipField 注入字段跳过判断函数。
var WithTimeField = serializer.WithTimeField
WithTimeField 注入时间字段判断函数。
Functions ¶
func GetRealIP ¶ added in v0.13.3
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地址
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))
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 Options ¶ added in v0.13.24
type Options = serializer.Options
Options 为序列化处理选项的别名,保持与原有 middleware 调用方式一致。