Move request limiter to middleware.

This commit is contained in:
2025-07-09 17:00:39 +08:00
parent 0021a73951
commit b568b234c4
11 changed files with 73 additions and 71 deletions

View File

@@ -3,11 +3,9 @@ package service
import (
"nysoure/server/dao"
"nysoure/server/model"
"nysoure/server/utils"
"regexp"
"strconv"
"strings"
"time"
"github.com/gofiber/fiber/v3/log"
)
@@ -19,10 +17,6 @@ const (
maxCommentBriefLength = 256 // Maximum length of a comment brief
)
var (
commentsLimiter = utils.NewRequestLimiter(maxCommentsPerIP, 24*time.Hour)
)
type CommentRequest struct {
Content string `json:"content"` // markdown
// Images []uint `json:"images"` // Unrequired after new design
@@ -62,11 +56,6 @@ func findImagesInContent(content string, host string) []uint {
}
func CreateComment(req CommentRequest, userID uint, refID uint, ip string, cType model.CommentType, host string) (*model.CommentView, error) {
if !commentsLimiter.AllowRequest(ip) {
log.Warnf("IP %s has exceeded the comment limit of %d comments per day", ip, maxCommentsPerIP)
return nil, model.NewRequestError("Too many comments from this IP address, please try again later")
}
if len(req.Content) == 0 {
return nil, model.NewRequestError("Content cannot be empty")
}