-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rate limit 100 requests / 10mins (#14)
* rate format * update
- Loading branch information
Showing
9 changed files
with
91 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/swuecho/chatgpt_backend/sqlc_queries" | ||
) | ||
|
||
// This function returns a middleware that limits requests from each user by their ID. | ||
func RateLimitByUserID(q *sqlc_queries.Queries) func(http.Handler) http.Handler { | ||
return func(next 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. | ||
ctx := r.Context() | ||
userIDStr := ctx.Value(userContextKey).(string) | ||
userIDInt, err := strconv.Atoi(userIDStr) | ||
if err != nil { | ||
http.Error(w, "Error: '"+userIDStr+"' is not a valid user ID. Please enter a valid user ID.", http.StatusBadRequest) | ||
return | ||
} | ||
messageCount, err := q.GetChatMessagesCount(r.Context(), int32(userIDInt)) | ||
if err != nil { | ||
http.Error(w, "Error: Could not get message count.", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// Check if the request exceeds the rate limit. | ||
if messageCount > 100 { | ||
http.Error(w, "Too Many Requests", http.StatusTooManyRequests) | ||
return | ||
} | ||
// Call the next handler. | ||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"folders": [ | ||
{ | ||
"path": "web" | ||
}, | ||
{ | ||
"path": "api" | ||
}, | ||
{ | ||
"path": "e2e" | ||
} | ||
], | ||
"settings": {} | ||
} |