diff --git a/modules/structs/wssessions.go b/modules/structs/wssessions.go index ac49d33..472ba82 100644 --- a/modules/structs/wssessions.go +++ b/modules/structs/wssessions.go @@ -1,13 +1,13 @@ package structs -// swagger:model ExistsUserWebSocketSessionsResponse -type ExistsUserWebSocketSessionsResponse struct { +// swagger:model ExistsUserWebSocketSessionIdResponse +type ExistsUserWebSocketSessionIdResponse struct { // Returns whether the user websocket session already exists or not E bool } -// swagger:model GetUserWebSocketSessionsResponse -type GetUserWebSocketSessionsResponse struct { - // List of all websocket sessions of the user that are connected +// swagger:model GetUserWebSocketSessionIdsResponse +type GetUserWebSocketSessionIdsResponse struct { + // List of all websocket sessionIds of the user that are connected WebSocketSessions []string } diff --git a/routers/api/v1/wssessions/wssessions.go b/routers/api/v1/wssessions/wssessions.go index 4b46a63..5863f75 100644 --- a/routers/api/v1/wssessions/wssessions.go +++ b/routers/api/v1/wssessions/wssessions.go @@ -7,8 +7,8 @@ import ( "github.com/gofiber/fiber/v2/utils" ) -func AddUserWebSocketSession(c *fiber.Ctx) error { - // swagger:operation POST /wsconnections/:userId/:webSocketSession wsconnections wsconnectionsAddUserWebSocketSession +func AddUserWebSocketSessionId(c *fiber.Ctx) error { + // swagger:operation POST /wsconnections/:userId/:wsSessionId wsconnections wsconnectionsAddUserWebSocketSessionId // --- // summary: Add user websocket session // parameters: @@ -20,24 +20,24 @@ func AddUserWebSocketSession(c *fiber.Ctx) error { // description: Added user web socket session userId := utils.CopyString(c.Params("userId")) - webSocketSession := utils.CopyString(c.Params("webSocketSession")) + wsSessionId := utils.CopyString(c.Params("wsSessionId")) if webSocketSessions, ok := cache.WebSocketSessions[userId]; ok { - if isInList(userId, webSocketSession) { + if isInList(userId, wsSessionId) { return c.SendStatus(fiber.StatusUnprocessableEntity) } - cache.WebSocketSessions[userId] = append(webSocketSessions, webSocketSession) + cache.WebSocketSessions[userId] = append(webSocketSessions, wsSessionId) } else { - cache.WebSocketSessions[userId] = []string{webSocketSession} + cache.WebSocketSessions[userId] = []string{wsSessionId} } return c.SendStatus(fiber.StatusOK) } -func isInList(userId string, webSocketSession string) bool { +func isInList(userId string, wsSessionId string) bool { for _, item := range cache.WebSocketSessions[userId] { - if item == webSocketSession { + if item == wsSessionId { return true } @@ -45,8 +45,8 @@ func isInList(userId string, webSocketSession string) bool { return false } -func ExistsUserWebSocketSession(c *fiber.Ctx) error { - // swagger:operation GET /wsconnections/:userId/:webSocketSession wsconnections wsconnectionsExistsUserWebSocketSession +func ExistsUserWebSocketSessionId(c *fiber.Ctx) error { + // swagger:operation GET /wsconnections/:userId/:wsSessionId wsconnections wsconnectionsExistsUserWebSocketSessionId // --- // summary: Returns whether the user websocket session already exists // produces: @@ -59,11 +59,11 @@ func ExistsUserWebSocketSession(c *fiber.Ctx) error { // '200': // description: Added user web socket session - return c.JSON(structs.ExistsUserWebSocketSessionsResponse{E: isInList(c.Params("userId"), c.Params("webSocketSession"))}) + return c.JSON(structs.ExistsUserWebSocketSessionIdResponse{E: isInList(c.Params("userId"), c.Params("wsSessionId"))}) } -func GetUserWebSocketSessions(c *fiber.Ctx) error { - // swagger:operation GET /wsconnections/:userId wsconnections wsconnectionsGetUserWebSocketSessions +func GetUserWebSocketSessionIds(c *fiber.Ctx) error { + // swagger:operation GET /wsconnections/:userId wsconnections wsconnectionsGetUserWebSocketSessionIds // --- // summary: List of all websocket sessions of the user that are connected // produces: @@ -76,7 +76,7 @@ func GetUserWebSocketSessions(c *fiber.Ctx) error { // '200': // description: List of all websocket sessions of the user that are connected // schema: - // "$ref": "#/definitions/GetUserWebSocketSessionsResponse" + // "$ref": "#/definitions/GetUserWebSocketSessionIdsResponse" var list []string @@ -84,11 +84,11 @@ func GetUserWebSocketSessions(c *fiber.Ctx) error { list = webSocketSessions } - return c.JSON(structs.GetUserWebSocketSessionsResponse{WebSocketSessions: list}) + return c.JSON(structs.GetUserWebSocketSessionIdsResponse{WebSocketSessions: list}) } func RemoveConnection(c *fiber.Ctx) error { - // swagger:operation DELETE /wsconnections/:userId/:webSocketSession wsconnections wsconnectionsRemoveConnection + // swagger:operation DELETE /wsconnections/:userId/:wsSessionId wsconnections wsconnectionsRemoveConnection // --- // summary: Deletes a websocket session from the user // parameters: @@ -100,11 +100,11 @@ func RemoveConnection(c *fiber.Ctx) error { // description: Websocket session deleted userId := c.Params("userId") - webSocketSession := c.Params("webSocketSession") + wsSessionId := c.Params("wsSessionId") if webSocketSessions, ok := cache.WebSocketSessions[userId]; ok { for index, session := range webSocketSessions { - if session == webSocketSession { + if session == wsSessionId { cache.WebSocketSessions[userId] = remove(webSocketSessions, index) break } diff --git a/routers/router/router.go b/routers/router/router.go index c2bc68d..4da2f3e 100644 --- a/routers/router/router.go +++ b/routers/router/router.go @@ -10,10 +10,10 @@ func SetupRoutes(app *fiber.App) { v1 := app.Group("/v1") wsc := v1.Group("/wsconnections") - wsc.Post("/:userId/:webSocketSession", ApiKeyValidation, wssessions.AddUserWebSocketSession) - wsc.Get("/:userId/:webSocketSession", ApiKeyValidation, wssessions.ExistsUserWebSocketSession) - wsc.Get("/:userId", ApiKeyValidation, wssessions.GetUserWebSocketSessions) - wsc.Delete("/:userId/:webSocketSession", ApiKeyValidation, wssessions.RemoveConnection) + wsc.Post("/:userId/:wsSessionId", ApiKeyValidation, wssessions.AddUserWebSocketSessionId) + wsc.Get("/:userId/:wsSessionId", ApiKeyValidation, wssessions.ExistsUserWebSocketSessionId) + wsc.Get("/:userId", ApiKeyValidation, wssessions.GetUserWebSocketSessionIds) + wsc.Delete("/:userId/:wsSessionId", ApiKeyValidation, wssessions.RemoveConnection) } func ApiKeyValidation(c *fiber.Ctx) error {