better func naming
parent
66b1bc38d5
commit
a3a217f5fc
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue