mid

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package mid provides common Gin middleware: panic recovery, CORS, JWT authentication, rate limiting, traceId, and gRPC interceptors.

Index

Constants

View Source
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

func GetTraceID(c *gin.Context) string

GetTraceID extracts the trace ID from a Gin context.

func GetUID

func GetUID(c *gin.Context) int64

GetUID extracts the authenticated user ID from the 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.

Jump to

Keyboard shortcuts

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