File size: 548 Bytes
a8ebadf
542878f
 
 
a8ebadf
542878f
 
 
 
 
4c360fd
a1eed6b
 
 
 
542878f
 
4c360fd
a1eed6b
542878f
 
 
a1eed6b
542878f
 
 
 
 
 
 
4c360fd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package middlewares

import (
	"time"
	"net/http"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/limiter"
)


/*
 *  --- Rate Limiter Middleware ---
*/
func RateLimiterMiddleware() func(*fiber.Ctx) error {
	return limiter.New(limiter.Config{
		Max:        200,
		Expiration: (1 * time.Minute),

		KeyGenerator: func(c *fiber.Ctx) string {
			return c.IP()
		},

		LimitReached: func(c *fiber.Ctx) error {
			return c.Status(http.StatusTooManyRequests).JSON(fiber.Map{
				"error": "Rate limit exceeded",
			})
		},
	})
}