Spaces:
Running
Running
| package controllers | |
| import ( | |
| "log" | |
| http_error "whatsapp-backend/models/error" | |
| "whatsapp-backend/services" | |
| "whatsapp-backend/utils" | |
| "github.com/gin-gonic/gin" | |
| "github.com/google/uuid" | |
| ) | |
| type SocketController interface { | |
| HandleWebSocket(ctx *gin.Context) | |
| } | |
| type socketController struct { | |
| socketService services.SocketService | |
| } | |
| func NewSocketController(socketService services.SocketService) SocketController { | |
| return &socketController{socketService: socketService} | |
| } | |
| // HandleWebSocket godoc | |
| // @Summary Connect to WebSocket | |
| // @Description Upgrade HTTP connection to WebSocket for real-time updates | |
| // @Tags whatsapp | |
| // @Security BearerAuth | |
| // @Success 101 | |
| // @Router /whatsapp/socket [get] | |
| func (c *socketController) HandleWebSocket(ctx *gin.Context) { | |
| userID, exists := ctx.Get("user_id") | |
| if !exists { | |
| log.Println("[WebSocket] User ID not found in context") | |
| utils.SendResponse[any, any](ctx, nil, nil, http_error.UNAUTHORIZED) | |
| return | |
| } | |
| accountID := userID.(uuid.UUID) | |
| c.socketService.HandleConnection(ctx.Writer, ctx.Request, accountID) | |
| } | |