middleware

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppContextMiddleware

func AppContextMiddleware() gin.HandlerFunc

AppContextMiddleware extracts application context from request

func Auth

func Auth(jwtSecret string) gin.HandlerFunc

Auth validates JWT tokens and sets user context This middleware extracts the JWT from the Authorization header, validates it, and sets user information in the Gin context

func ContextMiddleware

func ContextMiddleware() gin.HandlerFunc

ContextMiddleware enriches request with context information

func CorrelationID

func CorrelationID() gin.HandlerFunc

CorrelationID adds a correlation ID to each request for tracing If the X-Correlation-ID header is present, it uses that value, otherwise it generates a new UUID

func DefaultMetrics

func DefaultMetrics(namespace string) gin.HandlerFunc

DefaultMetrics creates a metrics middleware with default collector The namespace parameter is used to prefix all metric names

func Logger

func Logger(log *logger.Logger) gin.HandlerFunc

Logger logs HTTP requests with detailed information It captures method, path, query, status code, latency, IP, and user agent

func Metrics

func Metrics(collector *MetricsCollector) gin.HandlerFunc

Metrics creates a middleware that collects Prometheus metrics

func OptionalAuth

func OptionalAuth(jwtSecret string) gin.HandlerFunc

OptionalAuth is similar to Auth but doesn't require authentication If a valid token is provided, it sets the user context, otherwise continues without it

func PerIP

func PerIP(rps float64, burst int) gin.HandlerFunc

PerIP creates a rate limiting middleware that limits by IP address

func PerTenant

func PerTenant(rps float64, burst int) gin.HandlerFunc

PerTenant creates a rate limiting middleware that limits by tenant ID

func PerUser

func PerUser(rps float64, burst int) gin.HandlerFunc

PerUser creates a rate limiting middleware that limits by user ID

func RateLimit

func RateLimit(rps float64, burst int, keyFunc func(*gin.Context) string) gin.HandlerFunc

RateLimit creates a generic rate limiting middleware with a custom key extractor

func Recovery

func Recovery(log *logger.Logger) gin.HandlerFunc

Recovery provides panic recovery with proper logging It catches panics, logs them, and returns a standardized error response

func RecoveryWithStack

func RecoveryWithStack(log *logger.Logger) gin.HandlerFunc

RecoveryWithStack provides panic recovery with stack trace

func RequestContextMiddleware

func RequestContextMiddleware() gin.HandlerFunc

RequestContextMiddleware builds full request context after auth

func RequestLogger

func RequestLogger(log *logger.Logger) gin.HandlerFunc

RequestLogger logs incoming requests before processing

func RequestSizeLimit

func RequestSizeLimit(maxBytes int64) gin.HandlerFunc

RequestSizeLimit limits the size of incoming requests

func RequestValidation

func RequestValidation() gin.HandlerFunc

RequestValidation validates incoming requests It ensures Content-Type header is present for non-GET/DELETE requests

func RequireAllRoles

func RequireAllRoles(roles ...string) gin.HandlerFunc

RequireAllRoles checks if user has all of the required roles This middleware ensures the authenticated user has all of the specified roles

func RequireAnyPermission

func RequireAnyPermission(permissions ...string) gin.HandlerFunc

RequireAnyPermission creates middleware that checks for any of the permissions

func RequireContentType

func RequireContentType(contentType string) gin.HandlerFunc

RequireContentType validates that the request has the specified content type

func RequireJSON

func RequireJSON() gin.HandlerFunc

RequireJSON ensures the request has JSON content type

func RequirePermission

func RequirePermission(permission string) gin.HandlerFunc

RequirePermission creates middleware that checks for specific permission

func RequireRole

func RequireRole(role string) gin.HandlerFunc

RequireRole creates middleware that checks for specific role

func RequireRoles

func RequireRoles(roles ...string) gin.HandlerFunc

RequireRoles checks if user has any of the required roles This middleware ensures the authenticated user has at least one of the specified roles

func RequireSuperAdmin

func RequireSuperAdmin() gin.HandlerFunc

RequireSuperAdmin creates middleware that requires super admin role

func RequireTenantAdmin

func RequireTenantAdmin() gin.HandlerFunc

RequireTenantAdmin creates middleware that requires tenant admin role

func TenantScope

func TenantScope() gin.HandlerFunc

TenantScope ensures tenant context exists This middleware checks if a tenant ID is present in the context, either from JWT claims or from the X-Tenant-ID header

func Timeout

func Timeout(timeout time.Duration) gin.HandlerFunc

Timeout adds a timeout to requests If a request takes longer than the specified duration, it returns a timeout error

func TimeoutWithCustomMessage

func TimeoutWithCustomMessage(timeout time.Duration, message string) gin.HandlerFunc

TimeoutWithCustomMessage adds a timeout with a custom error message

Types

type MetricsCollector

type MetricsCollector struct {
	RequestsTotal   *prometheus.CounterVec
	RequestDuration *prometheus.HistogramVec
	ActiveRequests  prometheus.Gauge
}

MetricsCollector holds Prometheus metrics

func NewMetricsCollector

func NewMetricsCollector(namespace string) *MetricsCollector

NewMetricsCollector creates a new metrics collector with default metrics

func (*MetricsCollector) Register

func (mc *MetricsCollector) Register() error

Register registers all metrics with Prometheus

type RateLimiter

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

RateLimiter implements rate limiting with automatic cleanup

func NewRateLimiter

func NewRateLimiter(rps float64, burst int) *RateLimiter

NewRateLimiter creates a new rate limiter rps: requests per second burst: maximum burst size

func (*RateLimiter) CleanupLimiters

func (rl *RateLimiter) CleanupLimiters(ctx context.Context)

CleanupLimiters removes inactive limiters periodically

func (*RateLimiter) GetLimiter

func (rl *RateLimiter) GetLimiter(key string) *rate.Limiter

GetLimiter returns a limiter for the given key

func (*RateLimiter) Stop

func (rl *RateLimiter) Stop()

Stop stops the cleanup goroutine

Jump to

Keyboard shortcuts

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