mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-09-09 05:08:27 +00:00
bee8458a2d
* feat: add rate limit middleware * chore: update vendor dir * chore: update readme with new dependency * chore: add rate limit infos to swagger.md file * refactor: add ipv6 mask limiter option Add IPv6 CIDR /64 mask * refactor: increase rate limit to 1000 Address https://github.com/superseriousbusiness/gotosocial/pull/741#discussion_r945584800 Co-authored-by: tobi <31960611+tsmethurst@users.noreply.github.com>
71 lines
2 KiB
Go
71 lines
2 KiB
Go
package gin
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Option is used to define Middleware configuration.
|
|
type Option interface {
|
|
apply(*Middleware)
|
|
}
|
|
|
|
type option func(*Middleware)
|
|
|
|
func (o option) apply(middleware *Middleware) {
|
|
o(middleware)
|
|
}
|
|
|
|
// ErrorHandler is an handler used to inform when an error has occurred.
|
|
type ErrorHandler func(c *gin.Context, err error)
|
|
|
|
// WithErrorHandler will configure the Middleware to use the given ErrorHandler.
|
|
func WithErrorHandler(handler ErrorHandler) Option {
|
|
return option(func(middleware *Middleware) {
|
|
middleware.OnError = handler
|
|
})
|
|
}
|
|
|
|
// DefaultErrorHandler is the default ErrorHandler used by a new Middleware.
|
|
func DefaultErrorHandler(c *gin.Context, err error) {
|
|
panic(err)
|
|
}
|
|
|
|
// LimitReachedHandler is an handler used to inform when the limit has exceeded.
|
|
type LimitReachedHandler func(c *gin.Context)
|
|
|
|
// WithLimitReachedHandler will configure the Middleware to use the given LimitReachedHandler.
|
|
func WithLimitReachedHandler(handler LimitReachedHandler) Option {
|
|
return option(func(middleware *Middleware) {
|
|
middleware.OnLimitReached = handler
|
|
})
|
|
}
|
|
|
|
// DefaultLimitReachedHandler is the default LimitReachedHandler used by a new Middleware.
|
|
func DefaultLimitReachedHandler(c *gin.Context) {
|
|
c.String(http.StatusTooManyRequests, "Limit exceeded")
|
|
}
|
|
|
|
// KeyGetter will define the rate limiter key given the gin Context.
|
|
type KeyGetter func(c *gin.Context) string
|
|
|
|
// WithKeyGetter will configure the Middleware to use the given KeyGetter.
|
|
func WithKeyGetter(handler KeyGetter) Option {
|
|
return option(func(middleware *Middleware) {
|
|
middleware.KeyGetter = handler
|
|
})
|
|
}
|
|
|
|
// DefaultKeyGetter is the default KeyGetter used by a new Middleware.
|
|
// It returns the Client IP address.
|
|
func DefaultKeyGetter(c *gin.Context) string {
|
|
return c.ClientIP()
|
|
}
|
|
|
|
// WithExcludedKey will configure the Middleware to ignore key(s) using the given function.
|
|
func WithExcludedKey(handler func(string) bool) Option {
|
|
return option(func(middleware *Middleware) {
|
|
middleware.ExcludedKey = handler
|
|
})
|
|
}
|