From d7da8f133146de4fba1db13bfbc63242c917f817 Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Thu, 31 Oct 2024 22:37:31 +0300 Subject: backend: add rate limiting --- backend/api/rate.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 backend/api/rate.go (limited to 'backend/api/rate.go') 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 @@ +package api + +import ( + "net/http" + + "golang.org/x/time/rate" + + "github.com/gin-gonic/gin" +) + +func RateLimit(c *gin.Context) { + limiter := rate.NewLimiter(1, 5) // don't know if this is too much or not enough tbh + if limiter.Allow() { + c.Next() + } else { + c.AbortWithStatusJSON(http.StatusTooManyRequests, gin.H{ + "error": "Rate limit exceeded", + }) + } +} -- cgit v1.2.3