File size: 887 Bytes
542878f
 
 
 
 
 
 
 
 
 
 
4c360fd
 
 
 
542878f
 
a1eed6b
 
 
 
 
 
542878f
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package proxy

import (
	"log"
	"net/http"
	"time"

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


/*
 *  --- Logging Middleware ---
*/
func LoggingMiddleware(c *fiber.Ctx) error {
	start := time.Now()
	err   := c.Next()

	log.Printf("[%s] %s %s - %d %s in %v", time.Now().Format("2006-01-02 15:04:05"),
		c.Method(), c.Path(), c.Response().StatusCode(),
		http.StatusText(c.Response().StatusCode()), time.Since(start))

	return err
}


/*
 *  --- 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",
			})
		},
	})
}