-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathoptions.go
73 lines (58 loc) · 2.21 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package stdlib
import (
"net/http"
"github.com/ulule/limiter/v3"
)
// 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(w http.ResponseWriter, r *http.Request, 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(w http.ResponseWriter, r *http.Request, err error) {
panic(err)
}
// LimitReachedHandler is an handler used to inform when the limit has exceeded.
type LimitReachedHandler func(w http.ResponseWriter, r *http.Request)
// 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(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Limit exceeded", http.StatusTooManyRequests)
}
// KeyGetter will define the rate limiter key given the gin Context.
type KeyGetter func(r *http.Request) 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(limiter *limiter.Limiter) func(r *http.Request) string {
return func(r *http.Request) string {
return limiter.GetIPKey(r)
}
}
// 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
})
}