fix(middleware): Correct rate limit header type conversion
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"bookhoard/internal/database"
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -76,12 +77,12 @@ func (m *DeviceAuthMiddleware) Authenticate(next echo.HandlerFunc) echo.HandlerF
|
||||
if !m.rateLimiter.CheckRateLimit(deviceID, requestType, config) {
|
||||
remaining := m.rateLimiter.GetRemainingRequests(deviceID, requestType, config)
|
||||
c.Response().Header().Set("X-RateLimit-Limit", "60")
|
||||
c.Response().Header().Set("X-RateLimit-Remaining", string(rune(remaining)))
|
||||
c.Response().Header().Set("X-RateLimit-Remaining", strconv.Itoa(remaining))
|
||||
c.Response().Header().Set("X-RateLimit-Reset", "60")
|
||||
return c.JSON(http.StatusTooManyRequests, map[string]string{
|
||||
"error": "rate limit exceeded",
|
||||
"message": "Too many requests",
|
||||
"remaining": string(rune(remaining)),
|
||||
"remaining": strconv.Itoa(remaining),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -102,7 +103,6 @@ func (m *DeviceAuthMiddleware) Authenticate(next echo.HandlerFunc) echo.HandlerF
|
||||
c.Set("device", device)
|
||||
c.Set("device_ctx", ctx)
|
||||
c.Set("device_id", device.ID.Bytes)
|
||||
c.Set("user_id", device.UserID.Bytes)
|
||||
|
||||
return next(c)
|
||||
}
|
||||
@@ -165,9 +165,9 @@ func (m *DeviceAuthMiddleware) UpdateLastSeen(next echo.HandlerFunc) echo.Handle
|
||||
return func(c echo.Context) error {
|
||||
err := next(c)
|
||||
|
||||
deviceID, ok := c.Get("device_id").(uuid.UUID)
|
||||
deviceIDBytes, ok := c.Get("device_id").([16]byte)
|
||||
if ok {
|
||||
pgDeviceID := pgtype.UUID{Bytes: [16]byte(deviceID), Valid: true}
|
||||
pgDeviceID := pgtype.UUID{Bytes: deviceIDBytes, Valid: true}
|
||||
m.db.UpdateDeviceLastSeen(c.Request().Context(), pgDeviceID)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user