handler

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMaxJSONBodyBytes int64 = 1 << 20 // 1 MiB

DefaultMaxJSONBodyBytes 是 BindJSON 的默认请求体上限。 入口层无上限读 JSON 会让任意 handler 暴露 OOM 面;需要更大请求体的业务可改用 BindJSONWithMaxBytes 显式声明上限。

View Source
const MaxPage = 10000

MaxPage GetPage 允许的最大页码(M-D 修复:防深分页 DoS)。 超大 page 产生超大 OFFSET,大表上为性能灾难/DoS 面。钳制到该上限; 需更深度遍历的业务应改用游标/keyset 分页(框架不内置,避免功能扩张)。

Variables

This section is empty.

Functions

func BadRequest

func BadRequest(c *gin.Context, msg string)

BadRequest 返回参数/请求错误响应。

委托 response.FailWithCode(CodeFail),遵循当前响应模式(ModeBusiness 下 HTTP 200, 错误信息通过 body 中的 code 表达;ModeREST 下 CodeFail 属业务失败不映射 HTTP 错误,仍 200), 并写入 RequestID——与 response 模式系统一致,不再硬编 HTTP 状态码、不再丢失链路追踪。

func BindJSON

func BindJSON(c *gin.Context, req any) error

BindJSON 绑定 JSON 请求

func BindJSONWithMaxBytes added in v1.3.0

func BindJSONWithMaxBytes(c *gin.Context, req any, maxBytes int64) error

BindJSONWithMaxBytes 绑定 JSON 请求,并限制最大 body 大小。 maxBytes<=0 时返回明确错误,避免 http.MaxBytesReader 的非正上限产生不可读语义。

func BindQuery

func BindQuery(c *gin.Context, req any) error

BindQuery 绑定 Query 参数

func FormBool

func FormBool(c *gin.Context, key string, def bool) bool

FormBool 获取 POST 表单参数中的布尔值(带默认值)

func FormFloat64

func FormFloat64(c *gin.Context, key string, def float64) float64

FormFloat64 获取 POST 表单参数中的 float64(带默认值)

func FormInt

func FormInt(c *gin.Context, key string, def int) int

FormInt 获取 POST 表单参数中的整数(带默认值)

func FormInt64

func FormInt64(c *gin.Context, key string, def int64) int64

FormInt64 获取 POST 表单参数中的 int64(带默认值)

func FormString

func FormString(c *gin.Context, key string, def string) string

FormString 获取 POST 表单参数中的字符串(带默认值)

func FormUint64

func FormUint64(c *gin.Context, key string, def uint64) uint64

FormUint64 获取 POST 表单参数中的 uint64(带默认值)

func GetIDFromPath

func GetIDFromPath(c *gin.Context, paramName string) (uint, bool)

GetIDFromPath 从路径获取 ID

func GetPage

func GetPage(c *gin.Context) (int, int)

GetPage 获取分页参数

func HealthCheck

func HealthCheck(c *gin.Context)

HealthCheck 健康检查 @Summary 健康检查 @Description 检查服务是否正常运行 @Tags 系统 @Accept json @Produce json @Success 200 {object} map[string]string @Router /health [get]

响应体与 router.RegisterHealthRoute 收敛为同一 schema(H8d): 恒 200 + {"status":"ok"},不走 response 业务信封,便于 K8s 探针直读。 需要依赖探活(mysql/redis…失败 503)时改用 router.RegisterHealthRoute 传入 checks。

func InternalError

func InternalError(c *gin.Context, msg string)

InternalError 返回服务器错误响应。

委托 response.ServerError(CodeServerError),遵循当前响应模式(ModeBusiness 下 HTTP 200, ModeREST 下 HTTP 500),并写入 RequestID——与 response 模式系统一致, 不再硬编 HTTP 状态码、不再丢失链路追踪。

func ParseInt

func ParseInt(s string, defaultValue int) int

ParseInt 解析整数

func PathInt

func PathInt(c *gin.Context, key string, def int) int

PathInt 从路径参数获取整数(带默认值)

func PathInt64

func PathInt64(c *gin.Context, key string, def int64) int64

PathInt64 从路径参数获取 int64(带默认值)

func PathUint64

func PathUint64(c *gin.Context, key string, def uint64) uint64

PathUint64 从路径参数获取 uint64(带默认值)

func QueryBool

func QueryBool(c *gin.Context, key string, def bool) bool

QueryBool 获取 Query 参数中的布尔值(带默认值)

func QueryFloat64

func QueryFloat64(c *gin.Context, key string, def float64) float64

QueryFloat64 获取 Query 参数中的 float64(带默认值)

func QueryInt

func QueryInt(c *gin.Context, key string, def int) int

QueryInt 获取 Query 参数中的整数(带默认值)

func QueryInt64

func QueryInt64(c *gin.Context, key string, def int64) int64

QueryInt64 获取 Query 参数中的 int64(带默认值)

Types

This section is empty.

Jump to

Keyboard shortcuts

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