Documentation
¶
Overview ¶
Package mid provides common Gin middleware: panic recovery, CORS, JWT authentication, rate limiting, traceId, and gRPC interceptors.
Index ¶
- Constants
- func CORS(allowOrigins ...string) gin.HandlerFunc
- func GRPCStreamClientInterceptor() grpc.StreamClientInterceptor
- func GRPCStreamServerInterceptor() grpc.StreamServerInterceptor
- func GRPCUnaryClientInterceptor() grpc.UnaryClientInterceptor
- func GRPCUnaryServerInterceptor() grpc.UnaryServerInterceptor
- func GetTraceID(c *gin.Context) string
- func GetUID(c *gin.Context) int64
- func JWT(cfg JWTConfig) gin.HandlerFunc
- func RateLimit(max int, window time.Duration) gin.HandlerFunc
- func Recovery() gin.HandlerFunc
- func Trace() gin.HandlerFunc
- type JWTConfig
Constants ¶
const TraceHeader = "X-Trace-Id"
Variables ¶
This section is empty.
Functions ¶
func CORS ¶
func CORS(allowOrigins ...string) gin.HandlerFunc
CORS returns a permissive CORS middleware.
func GRPCStreamClientInterceptor ¶ added in v0.2.0
func GRPCStreamClientInterceptor() grpc.StreamClientInterceptor
GRPCStreamClientInterceptor injects trace_id for outgoing stream calls.
func GRPCStreamServerInterceptor ¶ added in v0.2.0
func GRPCStreamServerInterceptor() grpc.StreamServerInterceptor
GRPCStreamServerInterceptor extracts trace_id for incoming stream calls.
func GRPCUnaryClientInterceptor ¶ added in v0.2.0
func GRPCUnaryClientInterceptor() grpc.UnaryClientInterceptor
GRPCUnaryClientInterceptor injects trace_id from context into gRPC metadata for outgoing unary calls. Use when dialing another service.
conn, _ := grpc.NewClient(addr, grpc.WithUnaryInterceptor(mid.GRPCUnaryClientInterceptor()))
func GRPCUnaryServerInterceptor ¶ added in v0.2.0
func GRPCUnaryServerInterceptor() grpc.UnaryServerInterceptor
GRPCUnaryServerInterceptor extracts trace_id from incoming gRPC metadata and stores it in the context. Use when registering a gRPC server.
grpc.NewServer(grpc.UnaryInterceptor(mid.GRPCUnaryServerInterceptor()))
func GetTraceID ¶ added in v0.2.0
GetTraceID extracts the trace ID from a Gin context.
func JWT ¶
func JWT(cfg JWTConfig) gin.HandlerFunc
JWT returns a Gin middleware that verifies Bearer tokens. On success it sets "uid" (int64) in the Gin context.
func RateLimit ¶
func RateLimit(max int, window time.Duration) gin.HandlerFunc
RateLimit returns a simple in-memory rate limiter middleware. max requests per window per client IP.
func Recovery ¶
func Recovery() gin.HandlerFunc
Recovery returns a middleware that recovers from panics and returns a 500.
func Trace ¶ added in v0.2.0
func Trace() gin.HandlerFunc
Trace returns a Gin middleware that extracts or generates a trace ID, stores it in the context, and sets the response header.
Types ¶
type JWTConfig ¶
type JWTConfig struct {
Secret string // HMAC signing key
// HeaderUID is an optional header name for pre-verified user ID
// (e.g. from an API gateway). If set and present, JWT parsing is skipped.
HeaderUID string // default: "" (disabled)
}
JWTConfig configures the JWT middleware.