mirror of
https://github.com/kataras/iris.git
synced 2025-12-24 05:17:03 +00:00
New Rate Limit middleware (still WIP though)
Former-commit-id: 99e282e4d400c83a56a808212d812cd701e1bcd8
This commit is contained in:
@@ -283,13 +283,15 @@ You can serve [quicktemplate](https://github.com/valyala/quicktemplate) and [her
|
||||
|
||||
### Miscellaneous
|
||||
|
||||
- [Rate Limit](miscellaneous/ratelimit/main.go) **NEW**
|
||||
- [HTTP Method Override](https://github.com/kataras/iris/blob/master/middleware/methodoverride/methodoverride_test.go)
|
||||
- [Request Logger](http_request/request-logger/main.go)
|
||||
* [log requests to a file](http_request/request-logger/request-logger-file/main.go)
|
||||
- [Recovery](miscellaneous/recover/main.go)
|
||||
- [Profiling (pprof)](miscellaneous/pprof/main.go)
|
||||
- [Internal Application File Logger](miscellaneous/file-logger/main.go)
|
||||
- [Google reCAPTCHA](miscellaneous/recaptcha/main.go)
|
||||
- [Google reCAPTCHA](miscellaneous/recaptcha/main.go)
|
||||
- [hCaptcha](miscellaneous/hcaptcha/main.go) **NEW**
|
||||
|
||||
### Community-based Handlers
|
||||
|
||||
|
||||
47
_examples/miscellaneous/ratelimit/main.go
Normal file
47
_examples/miscellaneous/ratelimit/main.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/middleware/rate"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
app.Logger().SetLevel("debug")
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
|
||||
// Register the rate limiter middleware at the root router.
|
||||
//
|
||||
// Fist and second input parameters:
|
||||
// Allow 1 request per second, with a maximum burst size of 5.
|
||||
//
|
||||
// Third optional variadic input parameter:
|
||||
// Can be a cleanup function.
|
||||
// Iris provides a cleanup function that will check for old entries and remove them.
|
||||
// You can customize it, e.g. check every 1 minute
|
||||
// if a client's last visit was 5 minutes ago ("old" entry)
|
||||
// and remove it from the memory.
|
||||
rateLimiter := rate.Limit(1, 5, rate.PurgeEvery(time.Minute, 5*time.Minute))
|
||||
app.Use(rateLimiter)
|
||||
|
||||
// Routes.
|
||||
app.Get("/", index)
|
||||
app.Get("/other", other)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
func index(ctx iris.Context) {
|
||||
ctx.HTML("<h1>Index Page</h1>")
|
||||
}
|
||||
|
||||
func other(ctx iris.Context) {
|
||||
ctx.HTML("<h1>Other Page</h1>")
|
||||
}
|
||||
Reference in New Issue
Block a user