From fc39b5e90fe708c76d1bbfae04793c229d5269b1 Mon Sep 17 00:00:00 2001 From: Julian Shen Date: Wed, 23 Nov 2016 11:38:31 +0800 Subject: [PATCH] Create README.md --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..ea764f0 --- /dev/null +++ b/README.md @@ -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.