mirror of
https://github.com/wgh136/nysoure.git
synced 2025-12-16 15:51:14 +00:00
Move request limiter to middleware.
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user