All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Add auth-svc /validate endpoint for token checking Add chat-svc with auth client and Redis task queue Add worker-svc chat handler for task processing Co-Authored-By: Claude Code <claude@anthropic.com>
70 lines
1.9 KiB
Go
70 lines
1.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.threesix.ai/jordan/sp4-debug-1770477266/pkg/app"
|
|
"git.threesix.ai/jordan/sp4-debug-1770477266/pkg/auth"
|
|
"git.threesix.ai/jordan/sp4-debug-1770477266/pkg/httperror"
|
|
"git.threesix.ai/jordan/sp4-debug-1770477266/pkg/httpresponse"
|
|
"git.threesix.ai/jordan/sp4-debug-1770477266/pkg/logging"
|
|
"git.threesix.ai/jordan/sp4-debug-1770477266/services/chat-svc/internal/taskqueue"
|
|
)
|
|
|
|
// Chat handles chat message endpoints.
|
|
type Chat struct {
|
|
producer *taskqueue.Producer
|
|
logger *logging.Logger
|
|
}
|
|
|
|
// NewChat creates a new Chat handler.
|
|
func NewChat(producer *taskqueue.Producer, logger *logging.Logger) *Chat {
|
|
return &Chat{
|
|
producer: producer,
|
|
logger: logger.WithComponent("ChatHandler"),
|
|
}
|
|
}
|
|
|
|
// SendRequest is the request body for sending a chat message.
|
|
type SendRequest struct {
|
|
Message string `json:"message" validate:"required,min=1,max=5000"`
|
|
}
|
|
|
|
// SendResponse is returned after a message is queued for processing.
|
|
type SendResponse struct {
|
|
JobID string `json:"job_id"`
|
|
Status string `json:"status"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
// Send accepts a chat message and pushes it to the worker queue for processing.
|
|
func (h *Chat) Send(w http.ResponseWriter, r *http.Request) error {
|
|
if h.producer == nil {
|
|
return httperror.ServiceUnavailable("task queue not configured")
|
|
}
|
|
|
|
var req SendRequest
|
|
if err := app.BindAndValidate(r, &req); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Get authenticated user from context
|
|
user := auth.GetUser(r.Context())
|
|
if user == nil {
|
|
return httperror.Unauthorized("authentication required")
|
|
}
|
|
|
|
jobID, err := h.producer.EnqueueChatProcess(r.Context(), user.ID, req.Message)
|
|
if err != nil {
|
|
h.logger.Error("failed to enqueue chat message", "error", err, "user_id", user.ID)
|
|
return httperror.Internal("failed to queue message for processing")
|
|
}
|
|
|
|
httpresponse.Accepted(w, r, SendResponse{
|
|
JobID: jobID,
|
|
Status: "queued",
|
|
Message: "message queued for processing",
|
|
})
|
|
return nil
|
|
}
|