From c24ee6b67dc67eb58df00fa1ebb5a11d96e41639 Mon Sep 17 00:00:00 2001 From: Hao Wu Date: Thu, 23 Oct 2025 21:06:27 +0800 Subject: [PATCH] Improve rate limiting to match multiple chat endpoints - Use string suffix matching for chat endpoints instead of exact path matching - Add support for /chatbot endpoint in rate limiting - Reorganize imports for better readability --- api/middleware_rateLimit.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/api/middleware_rateLimit.go b/api/middleware_rateLimit.go index 646f0bcf..c70c4a0d 100644 --- a/api/middleware_rateLimit.go +++ b/api/middleware_rateLimit.go @@ -2,8 +2,10 @@ package main import ( "fmt" - "github.com/swuecho/chat_backend/sqlc_queries" "net/http" + "strings" + + "github.com/swuecho/chat_backend/sqlc_queries" ) // This function returns a middleware that limits requests from each user by their ID. @@ -12,7 +14,8 @@ func RateLimitByUserID(q *sqlc_queries.Queries) func(http.Handler) http.Handler return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Get the user ID from the request, e.g. from a JWT token. - if r.URL.Path == "/chat" || r.URL.Path == "/chat_stream" { + path := r.URL.Path + if strings.HasSuffix(path, "/chat") || strings.HasSuffix(path, "/chat_stream") || strings.HasSuffix(path, "/chatbot") { ctx := r.Context() userIDInt, err := getUserID(ctx) // role := ctx.Value(roleContextKey).(string)