Documentation
¶
Index ¶
- Constants
- Variables
- func DefaultRateLimitKeyExtractor(c *gin.Context) (tier, key string, err error)
- func GetRealIP(c *gin.Context) string
- func Metrics(name string, args ...any) gin.HandlerFunc
- func ResponseCsv(c *gin.Context, data CsvData)
- func ResponseError(c *gin.Context, err error)
- func ResponseSuccess(c *gin.Context, data any)
- func Wrap(controller func(c *gin.Context) (any, error)) gin.HandlerFunc
- type CsvData
- type Jwt
- func (j *Jwt[T, KEY]) ClaimsFromContext(c *gin.Context) (data T, err error)
- func (j *Jwt[T, KEY]) Config() JwtConfig
- func (j *Jwt[T, KEY]) Generate(data T, optNow ...time.Time) (string, string, error)
- func (j *Jwt[T, KEY]) Middleware(c *gin.Context)
- func (j *Jwt[T, KEY]) RawClaimsFromContext(c *gin.Context) (*JwtClaims[T], error)
- func (j *Jwt[T, KEY]) Validate(tokenString string) (*JwtClaims[T], error)
- type JwtClaims
- type JwtConfig
- type PrivateKey
- type RateLimitKeyExtractorFunc
- type RateLimitManager
- type SigningMethod
Constants ¶
const HttpStatusInternalError = 600
const RateLimitTierFree = "free"
Variables ¶
var SigningMethods struct { // RSA RS256 SigningMethod[*rsa.PrivateKey] // EdDSA EdDSA SigningMethod[ed25519.PrivateKey] }
Functions ¶
func DefaultRateLimitKeyExtractor ¶ added in v0.7.10
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
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 ResponseCsv ¶
func ResponseError ¶
func ResponseSuccess ¶
Types ¶
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 ¶
ClaimsFromContext returns JWT claims that injected via middleware.
func (*Jwt[T, KEY]) Middleware ¶
Middleware validates JWT token from HTTP Authorization header that in format of 'Bearer xxx'.
func (*Jwt[T, KEY]) RawClaimsFromContext ¶ added in v0.6.6
ClaimsFromContext returns JWT claims that injected via middleware.
type JwtClaims ¶
type JwtClaims[T any] struct { jwt.RegisteredClaims Data T }
type PrivateKey ¶
type PrivateKey interface {
Public() crypto.PublicKey
Equal(x crypto.PrivateKey) bool
}
type RateLimitKeyExtractorFunc ¶ added in v0.7.10
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 }