middleware

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

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

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

func GetClientIP

func GetClientIP(ctx context.Context) string

GetClientIP 获取客户端 IP

func GetRequestID

func GetRequestID(ctx context.Context) string

GetRequestID 获取请求 ID

func GetSpanID

func GetSpanID(ctx context.Context) string

GetSpanID 获取 Span ID

func GetTenantID

func GetTenantID(ctx context.Context) (string, bool)

GetTenantID 获取租户 ID

func GetTraceID

func GetTraceID(ctx context.Context) string

GetTraceID 获取追踪 ID

func GetUserID

func GetUserID(ctx context.Context) (uint64, bool)

GetUserID 获取用户 ID

func GetValue

func GetValue[T any](ctx context.Context, key ctxKey) (T, bool)

GetValue 获取上下文值

func WithValue

func WithValue(ctx context.Context, key ctxKey, value any) context.Context

WithValue 设置上下文值

Types

type GRPCChain

type GRPCChain struct {
	// contains filtered or unexported fields
}

GRPCChain gRPC 拦截器链构建器

func NewGRPCChain

func NewGRPCChain() *GRPCChain

NewGRPCChain 创建 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 返回一元拦截器列表

func (*GRPCChain) UseStream

func (c *GRPCChain) UseStream(interceptors ...grpc.StreamServerInterceptor) *GRPCChain

UseStream 添加流式拦截器

func (*GRPCChain) UseUnary

func (c *GRPCChain) UseUnary(interceptors ...grpc.UnaryServerInterceptor) *GRPCChain

UseUnary 添加一元拦截器

type GinChain

type GinChain struct {
	// contains filtered or unexported fields
}

GinChain Gin 中间件链构建器

func NewGinChain

func NewGinChain(middlewares ...gin.HandlerFunc) *GinChain

NewGinChain 创建 Gin 中间件链

func (*GinChain) Apply

func (c *GinChain) Apply(r *gin.Engine)

Apply 应用到 gin.Engine

func (*GinChain) Then

func (c *GinChain) Then() []gin.HandlerFunc

Then 返回最终的中间件列表

func (*GinChain) Use

func (c *GinChain) Use(middlewares ...gin.HandlerFunc) *GinChain

Use 添加中间件

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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