feat(httpapi): require Idempotency-Key on POST chat messages
This commit is contained in:
@@ -61,8 +61,7 @@ func main() {
|
|||||||
userSvc := service.NewUserService(store.Users(), hasher)
|
userSvc := service.NewUserService(store.Users(), hasher)
|
||||||
sessionSvc := service.NewSessionService(store.Users(), sessionRepo, hasher, cfg.SessionTTL)
|
sessionSvc := service.NewSessionService(store.Users(), sessionRepo, hasher, cfg.SessionTTL)
|
||||||
chatSvc := service.NewChatService(store.Chats(), store.Users())
|
chatSvc := service.NewChatService(store.Chats(), store.Users())
|
||||||
messageSvc := service.NewMessageService(store.Messages(), store.Chats())
|
messageSvc := service.NewMessageService(store.Messages(), store.Chats(), cfg.IdempotencyTTL)
|
||||||
|
|
||||||
healthChecks := []handler.HealthCheck{
|
healthChecks := []handler.HealthCheck{
|
||||||
func(ctx context.Context) error { return pool.Ping(ctx) },
|
func(ctx context.Context) error { return pool.Ping(ctx) },
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
@@ -10,6 +11,8 @@ import (
|
|||||||
"high-loud-apps-labs/internal/service"
|
"high-loud-apps-labs/internal/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||||
|
|
||||||
type MessageHandler struct {
|
type MessageHandler struct {
|
||||||
messages *service.MessageService
|
messages *service.MessageService
|
||||||
}
|
}
|
||||||
@@ -25,6 +28,14 @@ func (h *MessageHandler) Create(c *gin.Context) {
|
|||||||
return
|
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)
|
me := middleware.CurrentUser(c)
|
||||||
|
|
||||||
chatID, err := parseUUIDPathParam(c, "chat_id")
|
chatID, err := parseUUIDPathParam(c, "chat_id")
|
||||||
@@ -33,12 +44,20 @@ func (h *MessageHandler) Create(c *gin.Context) {
|
|||||||
return
|
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 {
|
if err != nil {
|
||||||
respondError(c, err)
|
respondError(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusCreated, dto.MessageFromDomain(m))
|
|
||||||
|
c.Data(result.Status, "application/json", result.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *MessageHandler) List(c *gin.Context) {
|
func (h *MessageHandler) List(c *gin.Context) {
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
@@ -9,18 +11,29 @@ import (
|
|||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
"high-loud-apps-labs/internal/domain"
|
"high-loud-apps-labs/internal/domain"
|
||||||
|
"high-loud-apps-labs/internal/httpapi/dto"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MessageService struct {
|
type MessageService struct {
|
||||||
messages domain.MessageRepository
|
messages domain.MessageRepository
|
||||||
chats domain.ChatRepository
|
chats domain.ChatRepository
|
||||||
|
idempotencyTTL time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMessageService(messages domain.MessageRepository, chats domain.ChatRepository) *MessageService {
|
func NewMessageService(
|
||||||
return &MessageService{messages: messages, chats: chats}
|
messages domain.MessageRepository,
|
||||||
|
chats domain.ChatRepository,
|
||||||
|
idempotencyTTL time.Duration,
|
||||||
|
) *MessageService {
|
||||||
|
return &MessageService{
|
||||||
|
messages: messages,
|
||||||
|
chats: chats,
|
||||||
|
idempotencyTTL: idempotencyTTL,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *MessageService) Create(ctx context.Context, senderID, chatID, text string) (*domain.Message, error) {
|
func (s *MessageService) Create(ctx context.Context, senderID, chatID, text, idempotencyKey, requestPath string,
|
||||||
|
) (*domain.CreateIdempotentResult, error) {
|
||||||
if strings.TrimSpace(text) == "" {
|
if strings.TrimSpace(text) == "" {
|
||||||
return nil, &domain.ValidationError{
|
return nil, &domain.ValidationError{
|
||||||
Field: "text",
|
Field: "text",
|
||||||
@@ -38,17 +51,32 @@ func (s *MessageService) Create(ctx context.Context, senderID, chatID, text stri
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
m := &domain.Message{
|
fresh := &domain.Message{
|
||||||
ID: uuid.NewString(),
|
ID: uuid.NewString(),
|
||||||
ChatID: chatID,
|
ChatID: chatID,
|
||||||
SenderID: senderID,
|
SenderID: senderID,
|
||||||
Text: text,
|
Text: text,
|
||||||
CreatedAt: time.Now().UTC(),
|
CreatedAt: time.Now().UTC(),
|
||||||
}
|
}
|
||||||
if err := s.messages.Create(ctx, m); err != nil {
|
|
||||||
|
body, err := json.Marshal(dto.MessageFromDomain(fresh))
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return m, nil
|
|
||||||
|
return s.messages.CreateIdempotent(
|
||||||
|
ctx,
|
||||||
|
domain.IdempotencyScope{
|
||||||
|
UserID: senderID,
|
||||||
|
Method: http.MethodPost,
|
||||||
|
Path: requestPath,
|
||||||
|
Key: idempotencyKey,
|
||||||
|
},
|
||||||
|
domain.RequestHash(text),
|
||||||
|
fresh,
|
||||||
|
body,
|
||||||
|
s.idempotencyTTL,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *MessageService) List(ctx context.Context, userID, chatID string, limit int, cursor *string) ([]domain.Message, *string, error) {
|
func (s *MessageService) List(ctx context.Context, userID, chatID string, limit int, cursor *string) ([]domain.Message, *string, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user