Documentation
¶
Overview ¶
Package middleware 提供 HTTP/gRPC 中间件。
HTTP 中间件:
- CORS、认证、限流、超时
- 请求 ID、日志、恢复、幂等性
gRPC 拦截器:
- 链路追踪、指标采集
- 日志、恢复、认证
使用示例:
router.Use(middleware.RequestID()) router.Use(middleware.Logging(logger)) router.Use(middleware.Recovery())
Index ¶
- Constants
- func GetClientIP(ctx context.Context) string
- func GetRequestID(ctx context.Context) string
- func GetSpanID(ctx context.Context) string
- func GetTenantID(ctx context.Context) (string, bool)
- func GetTraceID(ctx context.Context) string
- func GetUserID(ctx context.Context) (uint64, bool)
- func GetValue[T any](ctx context.Context, key ctxKey) (T, bool)
- func WithValue(ctx context.Context, key ctxKey, value any) context.Context
- type GRPCChain
- func (c *GRPCChain) ServerOptions() []grpc.ServerOption
- func (c *GRPCChain) StreamInterceptors() []grpc.StreamServerInterceptor
- func (c *GRPCChain) UnaryInterceptors() []grpc.UnaryServerInterceptor
- func (c *GRPCChain) UseStream(interceptors ...grpc.StreamServerInterceptor) *GRPCChain
- func (c *GRPCChain) UseUnary(interceptors ...grpc.UnaryServerInterceptor) *GRPCChain
- type GinChain
Constants ¶
View Source
const ( RequestIDKey ctxKey = "request_id" TraceIDKey ctxKey = "trace_id" SpanIDKey ctxKey = "span_id" UserIDKey ctxKey = "user_id" TenantIDKey ctxKey = "tenant_id" ClientIPKey ctxKey = "client_ip" UserAgentKey ctxKey = "user_agent" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type GRPCChain ¶
type GRPCChain struct {
// contains filtered or unexported fields
}
GRPCChain gRPC 拦截器链构建器
func (*GRPCChain) ServerOptions ¶
func (c *GRPCChain) ServerOptions() []grpc.ServerOption
ServerOptions 返回 gRPC ServerOptions
func (*GRPCChain) StreamInterceptors ¶
func (c *GRPCChain) StreamInterceptors() []grpc.StreamServerInterceptor
StreamInterceptors 返回流式拦截器列表
func (*GRPCChain) UnaryInterceptors ¶
func (c *GRPCChain) UnaryInterceptors() []grpc.UnaryServerInterceptor
UnaryInterceptors 返回一元拦截器列表
type GinChain ¶
type GinChain struct {
// contains filtered or unexported fields
}
GinChain Gin 中间件链构建器
func NewGinChain ¶
func NewGinChain(middlewares ...gin.HandlerFunc) *GinChain
NewGinChain 创建 Gin 中间件链
Click to show internal directories.
Click to hide internal directories.