Files
ophion/internal/api/ratelimit.go
bigtux 5b662cf12f 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
2026-02-05 21:35:47 -03:00

28 lines
608 B
Go

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