diff options
Diffstat (limited to 'backend/api/rate.go')
| -rw-r--r-- | backend/api/rate.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/backend/api/rate.go b/backend/api/rate.go new file mode 100644 index 0000000..1e262af --- /dev/null +++ b/backend/api/rate.go | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | package api | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "net/http" | ||
| 5 | |||
| 6 | "golang.org/x/time/rate" | ||
| 7 | |||
| 8 | "github.com/gin-gonic/gin" | ||
| 9 | ) | ||
| 10 | |||
| 11 | func RateLimit(c *gin.Context) { | ||
| 12 | limiter := rate.NewLimiter(1, 5) // don't know if this is too much or not enough tbh | ||
| 13 | if limiter.Allow() { | ||
| 14 | c.Next() | ||
| 15 | } else { | ||
| 16 | c.AbortWithStatusJSON(http.StatusTooManyRequests, gin.H{ | ||
| 17 | "error": "Rate limit exceeded", | ||
| 18 | }) | ||
| 19 | } | ||
| 20 | } | ||