feat: Initial OPHION structure
- Go backend with Fiber framework - Agent for metrics collection - Docker Compose for self-hosted - Auth middleware (JWT + API Keys) - Rate limiting - Install script
This commit is contained in:
27
internal/api/ratelimit.go
Normal file
27
internal/api/ratelimit.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/limiter"
|
||||
)
|
||||
|
||||
func RateLimitMiddleware() fiber.Handler {
|
||||
return limiter.New(limiter.Config{
|
||||
Max: 100, // 100 requests
|
||||
Expiration: 1 * time.Minute, // per minute
|
||||
KeyGenerator: func(c *fiber.Ctx) string {
|
||||
// Use API key or IP for rate limiting
|
||||
if key := c.Locals("api_key"); key != nil {
|
||||
return key.(string)
|
||||
}
|
||||
return c.IP()
|
||||
},
|
||||
LimitReached: func(c *fiber.Ctx) error {
|
||||
return c.Status(429).JSON(fiber.Map{
|
||||
"error": "Rate limit exceeded",
|
||||
})
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user