-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from ulule/v2
Release v2.0.0
- Loading branch information
Showing
41 changed files
with
1,653 additions
and
833 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
language: go | ||
before_install: | ||
- go get github.com/stretchr/testify | ||
- go get github.com/garyburd/redigo/redis | ||
- go get github.com/ant0ine/go-json-rest/rest | ||
- go get github.com/patrickmn/go-cache | ||
- go get -u github.com/golang/dep/cmd/dep | ||
- dep ensure | ||
- go get -u github.com/alecthomas/gometalinter | ||
- gometalinter --install | ||
go: | ||
- 1.4 | ||
- 1.5 | ||
- 1.6 | ||
- 1.7 | ||
- 1.8 | ||
script: make test | ||
- 1.8.1 | ||
- 1.8.2 | ||
- 1.8.3 | ||
- 1.9 | ||
- 1.9.1 | ||
- tip | ||
script: | ||
- make test | ||
- make lint | ||
services: | ||
- redis-server | ||
env: | ||
- REDIS_DISABLE_BOOTSTRAP=true | ||
matrix: | ||
fast_finish: true | ||
allow_failures: | ||
- go: tip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
Primary contributors: | ||
|
||
Gilles Fabio <[email protected]> | ||
Florent Messa <[email protected]> | ||
Gilles FABIO <[email protected]> | ||
Florent MESSA <[email protected]> | ||
Thomas LE ROUX <[email protected]> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Gopkg.toml for github.com/ulule/limiter | ||
|
||
[[constraint]] | ||
name = "github.com/pkg/errors" | ||
version = "0.8.0" | ||
|
||
[[constraint]] | ||
name = "github.com/go-redis/redis" | ||
version = "6.5.6" | ||
|
||
[[constraint]] | ||
name = "github.com/gin-gonic/gin" | ||
version = "v1.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package gin | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
||
"github.com/ulule/limiter" | ||
) | ||
|
||
// Middleware is the middleware for basic http.Handler. | ||
type Middleware struct { | ||
Limiter *limiter.Limiter | ||
OnError ErrorHandler | ||
OnLimitReached LimitReachedHandler | ||
} | ||
|
||
// NewMiddleware return a new instance of a basic HTTP middleware. | ||
func NewMiddleware(limiter *limiter.Limiter, options ...Option) gin.HandlerFunc { | ||
middleware := &Middleware{ | ||
Limiter: limiter, | ||
OnError: DefaultErrorHandler, | ||
OnLimitReached: DefaultLimitReachedHandler, | ||
} | ||
|
||
for _, option := range options { | ||
option.apply(middleware) | ||
} | ||
|
||
return func(ctx *gin.Context) { | ||
middleware.Handle(ctx) | ||
} | ||
} | ||
|
||
// Handle gin request. | ||
func (middleware *Middleware) Handle(c *gin.Context) { | ||
context, err := middleware.Limiter.Get(c, limiter.GetIPKey(c.Request)) | ||
if err != nil { | ||
middleware.OnError(c, err) | ||
c.Abort() | ||
return | ||
} | ||
|
||
c.Header("X-RateLimit-Limit", strconv.FormatInt(context.Limit, 10)) | ||
c.Header("X-RateLimit-Remaining", strconv.FormatInt(context.Remaining, 10)) | ||
c.Header("X-RateLimit-Reset", strconv.FormatInt(context.Reset, 10)) | ||
|
||
if context.Reached { | ||
middleware.OnLimitReached(c) | ||
c.Abort() | ||
return | ||
} | ||
|
||
c.Next() | ||
} |
Oops, something went wrong.