forked from julianshen/gin-limiter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21a67a5
commit fc39b5e
Showing
1 changed file
with
23 additions
and
0 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,23 @@ | ||
# gin-limiter | ||
|
||
This is to add a middleware to [Gin framework](https://github.com/gin-gonic/gin) to support rate limiting. It wraps [Juju's ratelimit](https://github.com/juju/ratelimit) implemetation as a Gin middleware | ||
|
||
## Usage | ||
|
||
```go | ||
lm := limiter.NewRateLimiter(time.Minute, 10, func(ctx *gin.Context) (string, error) { | ||
key := ctx.Request.Header.Get("X-API-KEY") | ||
if key != "" { | ||
return key, nil | ||
} | ||
return "", errors.New("API key is missing") | ||
}) | ||
|
||
r.GET("/ping", lm.Middleware(), func(c *gin.Context) { | ||
c.JSON(200, gin.H{ | ||
"message": "pong", | ||
}) | ||
}) | ||
``` | ||
|
||
This means the URI "/ping" only allows 10 requests per minutes per X-API-KEY. The key can be not only header but also with your own rules. You can decide what you try to limit with by returning the key.For example, it's also ok to use cookie or client ip as the key. |