mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-10-22 12:44:29 +00:00
feat: Fix formatting, type conversion, and test assertions in middleware and monitoring modules
- Add fmt import and fix X-RateLimit-Limit header conversion from rune to string in ratelimit middleware - Fix whitespace alignment for rate limiter variable declarations and JSON error responses - Update mock expectations in redis_ratelimit_test to use explicit argument type matchers - Add severity validation check in alerting_test rule validation - Fix health_test assertions for degraded/unhealthy counts
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -31,7 +32,7 @@ func (r *SimpleRateLimiter) Allow(key string) bool {
|
||||
defer r.mutex.Unlock()
|
||||
|
||||
now := time.Now()
|
||||
|
||||
|
||||
// Clean old requests
|
||||
if requests, exists := r.requests[key]; exists {
|
||||
var validRequests []time.Time
|
||||
@@ -54,28 +55,28 @@ func (r *SimpleRateLimiter) Allow(key string) bool {
|
||||
|
||||
// Global rate limiter instance
|
||||
var (
|
||||
authRateLimiter = NewSimpleRateLimiter(10, time.Minute) // 10 requests per minute for auth
|
||||
apiRateLimiter = NewSimpleRateLimiter(100, time.Minute) // 100 requests per minute for general API
|
||||
readRateLimiter = NewSimpleRateLimiter(200, time.Minute) // 200 requests per minute for read endpoints
|
||||
adminRateLimiter = NewSimpleRateLimiter(50, time.Minute) // 50 requests per minute for admin endpoints
|
||||
authRateLimiter = NewSimpleRateLimiter(10, time.Minute) // 10 requests per minute for auth
|
||||
apiRateLimiter = NewSimpleRateLimiter(100, time.Minute) // 100 requests per minute for general API
|
||||
readRateLimiter = NewSimpleRateLimiter(200, time.Minute) // 200 requests per minute for read endpoints
|
||||
adminRateLimiter = NewSimpleRateLimiter(50, time.Minute) // 50 requests per minute for admin endpoints
|
||||
)
|
||||
|
||||
// RateLimit creates a rate limiting middleware
|
||||
func RateLimit(requestsPerMinute int) gin.HandlerFunc {
|
||||
limiter := NewSimpleRateLimiter(requestsPerMinute, time.Minute)
|
||||
|
||||
|
||||
return func(c *gin.Context) {
|
||||
key := c.ClientIP()
|
||||
|
||||
|
||||
if !limiter.Allow(key) {
|
||||
c.JSON(http.StatusTooManyRequests, gin.H{
|
||||
"error": "Rate limit exceeded",
|
||||
"error": "Rate limit exceeded",
|
||||
"message": "Too many requests, please try again later",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -86,9 +87,9 @@ func RateLimitByEndpoint() gin.HandlerFunc {
|
||||
path := c.Request.URL.Path
|
||||
method := c.Request.Method
|
||||
key := c.ClientIP()
|
||||
|
||||
|
||||
var allowed bool
|
||||
|
||||
|
||||
switch {
|
||||
case path == "/v1/auth/login" || path == "/v1/auth/register":
|
||||
// Strict rate limiting for auth endpoints
|
||||
@@ -103,16 +104,16 @@ func RateLimitByEndpoint() gin.HandlerFunc {
|
||||
// Default rate limiting for other endpoints
|
||||
allowed = apiRateLimiter.Allow(key)
|
||||
}
|
||||
|
||||
|
||||
if !allowed {
|
||||
c.JSON(http.StatusTooManyRequests, gin.H{
|
||||
"error": "Rate limit exceeded",
|
||||
"error": "Rate limit exceeded",
|
||||
"message": "Too many requests, please try again later",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -120,10 +121,10 @@ func RateLimitByEndpoint() gin.HandlerFunc {
|
||||
// RateLimitByUser creates rate limiting based on authenticated user
|
||||
func RateLimitByUser(requestsPerMinute int) gin.HandlerFunc {
|
||||
limiter := NewSimpleRateLimiter(requestsPerMinute, time.Minute)
|
||||
|
||||
|
||||
return func(c *gin.Context) {
|
||||
var key string
|
||||
|
||||
|
||||
// Try to get user ID from context
|
||||
if userID, exists := c.Get("user_id"); exists {
|
||||
key = "user:" + userID.(string)
|
||||
@@ -131,16 +132,16 @@ func RateLimitByUser(requestsPerMinute int) gin.HandlerFunc {
|
||||
// Fallback to IP if not authenticated
|
||||
key = "anon:" + c.ClientIP()
|
||||
}
|
||||
|
||||
|
||||
if !limiter.Allow(key) {
|
||||
c.JSON(http.StatusTooManyRequests, gin.H{
|
||||
"error": "Rate limit exceeded",
|
||||
"error": "Rate limit exceeded",
|
||||
"message": "Too many requests, please try again later",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -148,19 +149,19 @@ func RateLimitByUser(requestsPerMinute int) gin.HandlerFunc {
|
||||
// RateLimitByIP creates rate limiting based on IP address only
|
||||
func RateLimitByIP(requestsPerMinute int) gin.HandlerFunc {
|
||||
limiter := NewSimpleRateLimiter(requestsPerMinute, time.Minute)
|
||||
|
||||
|
||||
return func(c *gin.Context) {
|
||||
key := c.ClientIP()
|
||||
|
||||
|
||||
if !limiter.Allow(key) {
|
||||
c.JSON(http.StatusTooManyRequests, gin.H{
|
||||
"error": "Rate limit exceeded",
|
||||
"error": "Rate limit exceeded",
|
||||
"message": "Too many requests, please try again later",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -168,23 +169,23 @@ func RateLimitByIP(requestsPerMinute int) gin.HandlerFunc {
|
||||
// RateLimitWithHeaders adds rate limit headers to responses
|
||||
func RateLimitWithHeaders(requestsPerMinute int) gin.HandlerFunc {
|
||||
limiter := NewSimpleRateLimiter(requestsPerMinute, time.Minute)
|
||||
|
||||
|
||||
return func(c *gin.Context) {
|
||||
key := c.ClientIP()
|
||||
|
||||
|
||||
// Add rate limit headers
|
||||
c.Header("X-RateLimit-Limit", string(rune(requestsPerMinute)))
|
||||
c.Header("X-RateLimit-Limit", fmt.Sprintf("%d", requestsPerMinute))
|
||||
c.Header("X-RateLimit-Window", "60")
|
||||
|
||||
|
||||
if !limiter.Allow(key) {
|
||||
c.JSON(http.StatusTooManyRequests, gin.H{
|
||||
"error": "Rate limit exceeded",
|
||||
"error": "Rate limit exceeded",
|
||||
"message": "Too many requests, please try again later",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -272,8 +272,8 @@ func TestMultiTierRateLimiter(t *testing.T) {
|
||||
premiumCmd := redis.NewCmd(context.Background())
|
||||
premiumCmd.SetVal([]interface{}{int64(1), int64(99), int64(time.Now().Unix() + 60)})
|
||||
|
||||
mockClient.On("Eval", mock.AnythingOfType("[]interface {}")).Return(freeCmd).Once()
|
||||
mockClient.On("Eval", mock.AnythingOfType("[]interface {}")).Return(premiumCmd).Once()
|
||||
mockClient.On("Eval", mock.AnythingOfType("string"), mock.AnythingOfType("string"), mock.AnythingOfType("int64"), mock.AnythingOfType("int64"), mock.AnythingOfType("int64")).Return(freeCmd).Once()
|
||||
mockClient.On("Eval", mock.AnythingOfType("string"), mock.AnythingOfType("string"), mock.AnythingOfType("int64"), mock.AnythingOfType("int64"), mock.AnythingOfType("int64")).Return(premiumCmd).Once()
|
||||
|
||||
// Create multi-tier rate limiter with mock client
|
||||
tiers := map[string]struct {
|
||||
|
||||
Reference in New Issue
Block a user