forked from ulule/limiter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_gjr.go
45 lines (36 loc) · 1.14 KB
/
middleware_gjr.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
package limiter
import (
"strconv"
"github.com/ant0ine/go-json-rest/rest"
)
// GJRMiddleware is the go-json-rest middleware.
type GJRMiddleware struct {
Limiter *Limiter
}
// NewGJRMiddleware returns a new instance of go-json-rest middleware.
func NewGJRMiddleware(limiter *Limiter) *GJRMiddleware {
return &GJRMiddleware{
Limiter: limiter,
}
}
// MiddlewareFunc is the middleware method (handler).
func (m *GJRMiddleware) MiddlewareFunc(h rest.HandlerFunc) rest.HandlerFunc {
return func(w rest.ResponseWriter, r *rest.Request) {
context, err := m.Limiter.Get(GetIPKey(r.Request))
if err != nil {
panic(err)
}
w.Header().Add("X-RateLimit-Limit", strconv.FormatInt(context.Limit, 10))
w.Header().Add("X-RateLimit-Remaining", strconv.FormatInt(context.Remaining, 10))
w.Header().Add("X-RateLimit-Reset", strconv.FormatInt(context.Reset, 10))
// That can be useful to access rate limit context in views.
r.Env["ratelimit:limit"] = context.Limit
r.Env["ratelimit:remaining"] = context.Remaining
r.Env["ratelimit:reset"] = context.Reset
if context.Reached {
rest.Error(w, "Limit exceeded", 429)
return
}
h(w, r)
}
}