feat(httpapi): require Idempotency-Key on POST chat messages
This commit is contained in:
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
@@ -10,6 +11,8 @@ import (
|
||||
"high-loud-apps-labs/internal/service"
|
||||
)
|
||||
|
||||
var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||
|
||||
type MessageHandler struct {
|
||||
messages *service.MessageService
|
||||
}
|
||||
@@ -25,6 +28,14 @@ func (h *MessageHandler) Create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
key := c.GetHeader("Idempotency-Key")
|
||||
if key == "" || !idempotencyKeyPattern.MatchString(key) {
|
||||
respondError(c, &badRequestError{
|
||||
msg: "Idempotency-Key обязателен и должен соответствовать ^[A-Za-z0-9._:-]{1,128}$",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
me := middleware.CurrentUser(c)
|
||||
|
||||
chatID, err := parseUUIDPathParam(c, "chat_id")
|
||||
@@ -33,12 +44,20 @@ func (h *MessageHandler) Create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
m, err := h.messages.Create(c.Request.Context(), me.ID, chatID, req.Text)
|
||||
result, err := h.messages.Create(
|
||||
c.Request.Context(),
|
||||
me.ID,
|
||||
chatID,
|
||||
req.Text,
|
||||
key,
|
||||
c.Request.URL.Path,
|
||||
)
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, dto.MessageFromDomain(m))
|
||||
|
||||
c.Data(result.Status, "application/json", result.Body)
|
||||
}
|
||||
|
||||
func (h *MessageHandler) List(c *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user