middleware

package
v0.0.0-...-742f5fa Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2026 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const ValidatedRequestKey = "validatedRequest"

Variables

This section is empty.

Functions

func AdminOnly

func AdminOnly(allowedRoles ...string) gin.HandlerFunc

AdminOnly 管理员权限中间件 注意:此中间件需要配合 Casbin 或用户角色系统使用 使用方法:在路由中添加 middleware.AdminOnly("admin") 来限制只有管理员可访问

func Auth

func Auth(cfg *config.Config) gin.HandlerFunc

Auth JWT 认证中间件

func CORS

func CORS() gin.HandlerFunc

CORS 跨域中间件

func GetRequest

func GetRequest[T any](c *gin.Context) (*T, error)

GetRequest 从上下文中获取已验证的请求对象

func InitSentry

func InitSentry(config SentryConfig) error

InitSentry 初始化 Sentry

func InitTracing

func InitTracing(config TracingConfig) (*sdktrace.TracerProvider, error)

InitTracing 初始化 OpenTelemetry

func Logger

func Logger() gin.HandlerFunc

Logger 日志中间件

func MustGetRequest

func MustGetRequest[T any](c *gin.Context) *T

MustGetRequest 从上下文中获取已验证的请求对象,如果不存在或类型不匹配则 panic

func Pprof

func Pprof(r *gin.Engine)

Pprof 注册 pprof 路由 只在需要时调用,建议通过配置控制是否启用

func RateLimit

func RateLimit() gin.HandlerFunc

RateLimit 限流中间件

func Recovery

func Recovery() gin.HandlerFunc

Recovery 恢复中间件,捕获 panic

func SecurityHeaders

func SecurityHeaders() gin.HandlerFunc

SecurityHeaders 安全响应头中间件

func Sentry

func Sentry() gin.HandlerFunc

Sentry 创建 Sentry 中间件

func Tracing

func Tracing(serviceName string) gin.HandlerFunc

Tracing 创建 OpenTelemetry 中间件

func Validation

func Validation(req interface{}) gin.HandlerFunc

Validation 通用请求参数绑定与验证中间件 req: 请求参数结构体的指针,例如 &dto.CreateUserRequest{} 该中间件会自动根据请求方法(GET/POST等)选择绑定 Query 或 JSON 并自动进行参数校验

Types

type SentryConfig

type SentryConfig struct {
	DSN              string
	Environment      string
	TracesSampleRate float64
	Debug            bool
}

SentryConfig Sentry 配置

type TracingConfig

type TracingConfig struct {
	ServiceName    string
	JaegerEndpoint string
	Enabled        bool
}

TracingConfig OpenTelemetry 配置

Jump to

Keyboard shortcuts

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