better func naming
parent
66b1bc38d5
commit
a3a217f5fc
|
@ -1,13 +1,13 @@
|
||||||
package structs
|
package structs
|
||||||
|
|
||||||
// swagger:model ExistsUserWebSocketSessionsResponse
|
// swagger:model ExistsUserWebSocketSessionIdResponse
|
||||||
type ExistsUserWebSocketSessionsResponse struct {
|
type ExistsUserWebSocketSessionIdResponse struct {
|
||||||
// Returns whether the user websocket session already exists or not
|
// Returns whether the user websocket session already exists or not
|
||||||
E bool
|
E bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// swagger:model GetUserWebSocketSessionsResponse
|
// swagger:model GetUserWebSocketSessionIdsResponse
|
||||||
type GetUserWebSocketSessionsResponse struct {
|
type GetUserWebSocketSessionIdsResponse struct {
|
||||||
// List of all websocket sessions of the user that are connected
|
// List of all websocket sessionIds of the user that are connected
|
||||||
WebSocketSessions []string
|
WebSocketSessions []string
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,8 +7,8 @@ import (
|
||||||
"github.com/gofiber/fiber/v2/utils"
|
"github.com/gofiber/fiber/v2/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AddUserWebSocketSession(c *fiber.Ctx) error {
|
func AddUserWebSocketSessionId(c *fiber.Ctx) error {
|
||||||
// swagger:operation POST /wsconnections/:userId/:webSocketSession wsconnections wsconnectionsAddUserWebSocketSession
|
// swagger:operation POST /wsconnections/:userId/:wsSessionId wsconnections wsconnectionsAddUserWebSocketSessionId
|
||||||
// ---
|
// ---
|
||||||
// summary: Add user websocket session
|
// summary: Add user websocket session
|
||||||
// parameters:
|
// parameters:
|
||||||
|
@ -20,24 +20,24 @@ func AddUserWebSocketSession(c *fiber.Ctx) error {
|
||||||
// description: Added user web socket session
|
// description: Added user web socket session
|
||||||
|
|
||||||
userId := utils.CopyString(c.Params("userId"))
|
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 webSocketSessions, ok := cache.WebSocketSessions[userId]; ok {
|
||||||
if isInList(userId, webSocketSession) {
|
if isInList(userId, wsSessionId) {
|
||||||
return c.SendStatus(fiber.StatusUnprocessableEntity)
|
return c.SendStatus(fiber.StatusUnprocessableEntity)
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.WebSocketSessions[userId] = append(webSocketSessions, webSocketSession)
|
cache.WebSocketSessions[userId] = append(webSocketSessions, wsSessionId)
|
||||||
} else {
|
} else {
|
||||||
cache.WebSocketSessions[userId] = []string{webSocketSession}
|
cache.WebSocketSessions[userId] = []string{wsSessionId}
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.SendStatus(fiber.StatusOK)
|
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] {
|
for _, item := range cache.WebSocketSessions[userId] {
|
||||||
if item == webSocketSession {
|
if item == wsSessionId {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,8 +45,8 @@ func isInList(userId string, webSocketSession string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExistsUserWebSocketSession(c *fiber.Ctx) error {
|
func ExistsUserWebSocketSessionId(c *fiber.Ctx) error {
|
||||||
// swagger:operation GET /wsconnections/:userId/:webSocketSession wsconnections wsconnectionsExistsUserWebSocketSession
|
// swagger:operation GET /wsconnections/:userId/:wsSessionId wsconnections wsconnectionsExistsUserWebSocketSessionId
|
||||||
// ---
|
// ---
|
||||||
// summary: Returns whether the user websocket session already exists
|
// summary: Returns whether the user websocket session already exists
|
||||||
// produces:
|
// produces:
|
||||||
|
@ -59,11 +59,11 @@ func ExistsUserWebSocketSession(c *fiber.Ctx) error {
|
||||||
// '200':
|
// '200':
|
||||||
// description: Added user web socket session
|
// 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 {
|
func GetUserWebSocketSessionIds(c *fiber.Ctx) error {
|
||||||
// swagger:operation GET /wsconnections/:userId wsconnections wsconnectionsGetUserWebSocketSessions
|
// swagger:operation GET /wsconnections/:userId wsconnections wsconnectionsGetUserWebSocketSessionIds
|
||||||
// ---
|
// ---
|
||||||
// summary: List of all websocket sessions of the user that are connected
|
// summary: List of all websocket sessions of the user that are connected
|
||||||
// produces:
|
// produces:
|
||||||
|
@ -76,7 +76,7 @@ func GetUserWebSocketSessions(c *fiber.Ctx) error {
|
||||||
// '200':
|
// '200':
|
||||||
// description: List of all websocket sessions of the user that are connected
|
// description: List of all websocket sessions of the user that are connected
|
||||||
// schema:
|
// schema:
|
||||||
// "$ref": "#/definitions/GetUserWebSocketSessionsResponse"
|
// "$ref": "#/definitions/GetUserWebSocketSessionIdsResponse"
|
||||||
|
|
||||||
var list []string
|
var list []string
|
||||||
|
|
||||||
|
@ -84,11 +84,11 @@ func GetUserWebSocketSessions(c *fiber.Ctx) error {
|
||||||
list = webSocketSessions
|
list = webSocketSessions
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.JSON(structs.GetUserWebSocketSessionsResponse{WebSocketSessions: list})
|
return c.JSON(structs.GetUserWebSocketSessionIdsResponse{WebSocketSessions: list})
|
||||||
}
|
}
|
||||||
|
|
||||||
func RemoveConnection(c *fiber.Ctx) error {
|
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
|
// summary: Deletes a websocket session from the user
|
||||||
// parameters:
|
// parameters:
|
||||||
|
@ -100,11 +100,11 @@ func RemoveConnection(c *fiber.Ctx) error {
|
||||||
// description: Websocket session deleted
|
// description: Websocket session deleted
|
||||||
|
|
||||||
userId := c.Params("userId")
|
userId := c.Params("userId")
|
||||||
webSocketSession := c.Params("webSocketSession")
|
wsSessionId := c.Params("wsSessionId")
|
||||||
|
|
||||||
if webSocketSessions, ok := cache.WebSocketSessions[userId]; ok {
|
if webSocketSessions, ok := cache.WebSocketSessions[userId]; ok {
|
||||||
for index, session := range webSocketSessions {
|
for index, session := range webSocketSessions {
|
||||||
if session == webSocketSession {
|
if session == wsSessionId {
|
||||||
cache.WebSocketSessions[userId] = remove(webSocketSessions, index)
|
cache.WebSocketSessions[userId] = remove(webSocketSessions, index)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,10 +10,10 @@ func SetupRoutes(app *fiber.App) {
|
||||||
v1 := app.Group("/v1")
|
v1 := app.Group("/v1")
|
||||||
|
|
||||||
wsc := v1.Group("/wsconnections")
|
wsc := v1.Group("/wsconnections")
|
||||||
wsc.Post("/:userId/:webSocketSession", ApiKeyValidation, wssessions.AddUserWebSocketSession)
|
wsc.Post("/:userId/:wsSessionId", ApiKeyValidation, wssessions.AddUserWebSocketSessionId)
|
||||||
wsc.Get("/:userId/:webSocketSession", ApiKeyValidation, wssessions.ExistsUserWebSocketSession)
|
wsc.Get("/:userId/:wsSessionId", ApiKeyValidation, wssessions.ExistsUserWebSocketSessionId)
|
||||||
wsc.Get("/:userId", ApiKeyValidation, wssessions.GetUserWebSocketSessions)
|
wsc.Get("/:userId", ApiKeyValidation, wssessions.GetUserWebSocketSessionIds)
|
||||||
wsc.Delete("/:userId/:webSocketSession", ApiKeyValidation, wssessions.RemoveConnection)
|
wsc.Delete("/:userId/:wsSessionId", ApiKeyValidation, wssessions.RemoveConnection)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ApiKeyValidation(c *fiber.Ctx) error {
|
func ApiKeyValidation(c *fiber.Ctx) error {
|
||||||
|
|
Loading…
Reference in New Issue