middleware

package
v0.7.11 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const HttpStatusInternalError = 600
View Source
const RateLimitTierFree = "free"

Variables

View Source
var SigningMethods struct {
	// RSA
	RS256 SigningMethod[*rsa.PrivateKey]

	// EdDSA
	EdDSA SigningMethod[ed25519.PrivateKey]
}

Functions

func DefaultRateLimitKeyExtractor added in v0.7.10

func DefaultRateLimitKeyExtractor(c *gin.Context) (tier, key string, err error)

DefaultRateLimitKeyExtractor is the default implementation of RateLimitKeyExtractorFunc, which uses the client's real IP address as the rate limit key and assigns it to the "free" tier.

func GetRealIP added in v0.7.10

func GetRealIP(c *gin.Context) string

GetRealIP retrieves the real IP address of the client from the gin context, considering the "X-Real-IP" header if present.

Note, Gin provides a method ClientIP() to get the client IP address, but it requires to set the trusted proxies. In this case, services have to change the proxy settings when proxy IP address changed.

As a result, the service depends on the devops to set the correct "X-Real-IP" header in the reverse proxy.

func Metrics

func Metrics(name string, args ...any) gin.HandlerFunc

func ResponseCsv

func ResponseCsv(c *gin.Context, data CsvData)

func ResponseError

func ResponseError(c *gin.Context, err error)

func ResponseSuccess

func ResponseSuccess(c *gin.Context, data any)

func Wrap

func Wrap(controller func(c *gin.Context) (any, error)) gin.HandlerFunc

Types

type CsvData

type CsvData struct {
	Filename string
	Data     [][]string
	// bom head is included by default, set true to exclude it.
	ExcludeBOMHeader bool
}

type Jwt

type Jwt[DATA any, KEY PrivateKey] struct {
	// contains filtered or unexported fields
}

func MustNewRandomJwt

func MustNewRandomJwt[T any, KEY PrivateKey](config JwtConfig, method SigningMethod[KEY]) *Jwt[T, KEY]

func NewJwt

func NewJwt[T any, KEY PrivateKey](config JwtConfig, method SigningMethod[KEY]) (*Jwt[T, KEY], error)

func (*Jwt[T, KEY]) ClaimsFromContext

func (j *Jwt[T, KEY]) ClaimsFromContext(c *gin.Context) (data T, err error)

ClaimsFromContext returns JWT claims that injected via middleware.

func (*Jwt[T, KEY]) Config

func (j *Jwt[T, KEY]) Config() JwtConfig

func (*Jwt[T, KEY]) Generate

func (j *Jwt[T, KEY]) Generate(data T, optNow ...time.Time) (string, string, error)

Generate generates access token and refresh token.

func (*Jwt[T, KEY]) Middleware

func (j *Jwt[T, KEY]) Middleware(c *gin.Context)

Middleware validates JWT token from HTTP Authorization header that in format of 'Bearer xxx'.

func (*Jwt[T, KEY]) RawClaimsFromContext added in v0.6.6

func (j *Jwt[T, KEY]) RawClaimsFromContext(c *gin.Context) (*JwtClaims[T], error)

ClaimsFromContext returns JWT claims that injected via middleware.

func (*Jwt[T, KEY]) Validate

func (j *Jwt[T, KEY]) Validate(tokenString string) (*JwtClaims[T], error)

Validate validates the specified JWT token and returns parsed claims.

type JwtClaims

type JwtClaims[T any] struct {
	jwt.RegisteredClaims

	Data T
}

type JwtConfig

type JwtConfig struct {
	Issuer  string
	Subject string

	AccessTokenTimeout  time.Duration `default:"10m"` // 10 minutes
	RefreshTokenTimeout time.Duration `default:"24h"` // 1 day

	Key string // private key in PEM format, key type could be ignored
}

type PrivateKey

type PrivateKey interface {
	Public() crypto.PublicKey
	Equal(x crypto.PrivateKey) bool
}

type RateLimitKeyExtractorFunc added in v0.7.10

type RateLimitKeyExtractorFunc func(c *gin.Context) (tier, key string, err error)

RateLimitKeyExtractorFunc defines how to extract the rate limit key from the gin context.

type RateLimitManager added in v0.7.10

type RateLimitManager struct {
	*rate.LimiterManager
	// contains filtered or unexported fields
}

RateLimitManager is a wrapper around rate.LimiterManager that provides a gin middleware for rate limit.

func NewRateLimitManager added in v0.7.10

func NewRateLimitManager(config rate.Config, keyExtractor ...RateLimitKeyExtractorFunc) *RateLimitManager

NewRateLimitManager creates a new RateLimitManager with the given configuration and key extractor function. If no key extractor function is provided, it defaults to using the DefaultRateLimitKeyExtractor.

func (*RateLimitManager) Middleware added in v0.7.10

func (manager *RateLimitManager) Middleware(api string) gin.HandlerFunc

Middleware returns a gin.HandlerFunc that applies rate limiting based on the provided API name.

type SigningMethod

type SigningMethod[T PrivateKey] interface {
	MustGenerateKey() T
	ToPEM(key T) []byte
	FromPEM(data []byte) (T, error)

	JwtSigningMethod() jwt.SigningMethod
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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