-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example how to use ulule-limiter with Iris web framework (#2152)
* Added example how to use ulule-limiter with Iris web framework * format main.go Signed-off-by: Gerasimos (Makis) Maropoulos <kataras2006@hotmail.com> --------- Signed-off-by: Gerasimos (Makis) Maropoulos <kataras2006@hotmail.com> Co-authored-by: Gerasimos (Makis) Maropoulos <kataras2006@hotmail.com>
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
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,68 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/kataras/iris/v12" | ||
|
||
"github.com/ulule/limiter/v3" | ||
"github.com/ulule/limiter/v3/drivers/store/memory" | ||
) | ||
|
||
func main() { | ||
app := iris.New() | ||
app.Get("/hello", IPRateLimit(), helloWorldHandler) // 3. Use middleware | ||
app.Run(iris.Addr(":8080")) | ||
} | ||
|
||
func helloWorldHandler(ctx iris.Context) { | ||
err := ctx.StopWithJSON(iris.StatusOK, iris.Map{ | ||
"message": "Hello World!", | ||
}) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
|
||
func IPRateLimit() iris.Handler { | ||
// 1. Configure | ||
rate := limiter.Rate{ | ||
Period: 2 * time.Second, | ||
Limit: 1, | ||
} | ||
store := memory.NewStore() | ||
ipRateLimiter := limiter.New(store, rate) | ||
|
||
// 2. Return middleware handler | ||
return func(ctx iris.Context) { | ||
ip := ctx.RemoteAddr() | ||
limiterCtx, err := ipRateLimiter.Get(ctx.Request().Context(), ip) | ||
if err != nil { | ||
log.Printf("IPRateLimit - ipRateLimiter.Get - err: %v, %s on %s", err, ip, ctx.Request().URL) | ||
ctx.StatusCode(http.StatusInternalServerError) | ||
ctx.JSON(iris.Map{ | ||
"success": false, | ||
"message": err, | ||
}) | ||
return | ||
} | ||
|
||
ctx.Header("X-RateLimit-Limit", strconv.FormatInt(limiterCtx.Limit, 10)) | ||
ctx.Header("X-RateLimit-Remaining", strconv.FormatInt(limiterCtx.Remaining, 10)) | ||
ctx.Header("X-RateLimit-Reset", strconv.FormatInt(limiterCtx.Reset, 10)) | ||
|
||
if limiterCtx.Reached { | ||
log.Printf("Too Many Requests from %s on %s", ip, ctx.Request().URL) | ||
ctx.StatusCode(http.StatusTooManyRequests) | ||
ctx.JSON(iris.Map{ | ||
"success": false, | ||
"message": "Too Many Requests on " + ctx.Request().URL.String(), | ||
}) | ||
return | ||
} | ||
ctx.Next() | ||
} | ||
} |