removed wssessions because switched to redis
parent
a3a217f5fc
commit
6dbfe967c0
|
@ -1,120 +0,0 @@
|
||||||
package wssessions
|
|
||||||
|
|
||||||
import (
|
|
||||||
"clickandjoin.app/managementsystem/modules/cache"
|
|
||||||
"clickandjoin.app/managementsystem/modules/structs"
|
|
||||||
"github.com/gofiber/fiber/v2"
|
|
||||||
"github.com/gofiber/fiber/v2/utils"
|
|
||||||
)
|
|
||||||
|
|
||||||
func AddUserWebSocketSessionId(c *fiber.Ctx) error {
|
|
||||||
// swagger:operation POST /wsconnections/:userId/:wsSessionId wsconnections wsconnectionsAddUserWebSocketSessionId
|
|
||||||
// ---
|
|
||||||
// summary: Add user websocket session
|
|
||||||
// parameters:
|
|
||||||
// - name: X-Ms-Api
|
|
||||||
// in: header
|
|
||||||
// required: true
|
|
||||||
// responses:
|
|
||||||
// '200':
|
|
||||||
// description: Added user web socket session
|
|
||||||
|
|
||||||
userId := utils.CopyString(c.Params("userId"))
|
|
||||||
wsSessionId := utils.CopyString(c.Params("wsSessionId"))
|
|
||||||
|
|
||||||
if webSocketSessions, ok := cache.WebSocketSessions[userId]; ok {
|
|
||||||
if isInList(userId, wsSessionId) {
|
|
||||||
return c.SendStatus(fiber.StatusUnprocessableEntity)
|
|
||||||
}
|
|
||||||
|
|
||||||
cache.WebSocketSessions[userId] = append(webSocketSessions, wsSessionId)
|
|
||||||
} else {
|
|
||||||
cache.WebSocketSessions[userId] = []string{wsSessionId}
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.SendStatus(fiber.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func isInList(userId string, wsSessionId string) bool {
|
|
||||||
for _, item := range cache.WebSocketSessions[userId] {
|
|
||||||
if item == wsSessionId {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExistsUserWebSocketSessionId(c *fiber.Ctx) error {
|
|
||||||
// swagger:operation GET /wsconnections/:userId/:wsSessionId wsconnections wsconnectionsExistsUserWebSocketSessionId
|
|
||||||
// ---
|
|
||||||
// summary: Returns whether the user websocket session already exists
|
|
||||||
// produces:
|
|
||||||
// - application/json
|
|
||||||
// parameters:
|
|
||||||
// - name: X-Ms-Api
|
|
||||||
// in: header
|
|
||||||
// required: true
|
|
||||||
// responses:
|
|
||||||
// '200':
|
|
||||||
// description: Added user web socket session
|
|
||||||
|
|
||||||
return c.JSON(structs.ExistsUserWebSocketSessionIdResponse{E: isInList(c.Params("userId"), c.Params("wsSessionId"))})
|
|
||||||
}
|
|
||||||
|
|
||||||
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:
|
|
||||||
// - application/json
|
|
||||||
// parameters:
|
|
||||||
// - name: X-Ms-Api
|
|
||||||
// in: header
|
|
||||||
// required: true
|
|
||||||
// responses:
|
|
||||||
// '200':
|
|
||||||
// description: List of all websocket sessions of the user that are connected
|
|
||||||
// schema:
|
|
||||||
// "$ref": "#/definitions/GetUserWebSocketSessionIdsResponse"
|
|
||||||
|
|
||||||
var list []string
|
|
||||||
|
|
||||||
if webSocketSessions, ok := cache.WebSocketSessions[c.Params("userId")]; ok {
|
|
||||||
list = webSocketSessions
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.JSON(structs.GetUserWebSocketSessionIdsResponse{WebSocketSessions: list})
|
|
||||||
}
|
|
||||||
|
|
||||||
func RemoveConnection(c *fiber.Ctx) error {
|
|
||||||
// swagger:operation DELETE /wsconnections/:userId/:wsSessionId wsconnections wsconnectionsRemoveConnection
|
|
||||||
// ---
|
|
||||||
// summary: Deletes a websocket session from the user
|
|
||||||
// parameters:
|
|
||||||
// - name: X-Ms-Api
|
|
||||||
// in: header
|
|
||||||
// required: true
|
|
||||||
// responses:
|
|
||||||
// '200':
|
|
||||||
// description: Websocket session deleted
|
|
||||||
|
|
||||||
userId := c.Params("userId")
|
|
||||||
wsSessionId := c.Params("wsSessionId")
|
|
||||||
|
|
||||||
if webSocketSessions, ok := cache.WebSocketSessions[userId]; ok {
|
|
||||||
for index, session := range webSocketSessions {
|
|
||||||
if session == wsSessionId {
|
|
||||||
cache.WebSocketSessions[userId] = remove(webSocketSessions, index)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.SendStatus(fiber.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func remove(s []string, i int) []string {
|
|
||||||
s[i] = s[0]
|
|
||||||
return s[1:]
|
|
||||||
}
|
|
|
@ -2,18 +2,11 @@ package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"clickandjoin.app/managementsystem/modules/config"
|
"clickandjoin.app/managementsystem/modules/config"
|
||||||
wssessions "clickandjoin.app/managementsystem/routers/api/v1/wssessions"
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetupRoutes(app *fiber.App) {
|
func SetupRoutes(app *fiber.App) {
|
||||||
v1 := app.Group("/v1")
|
//v1 := app.Group("/v1")
|
||||||
|
|
||||||
wsc := v1.Group("/wsconnections")
|
|
||||||
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 {
|
func ApiKeyValidation(c *fiber.Ctx) error {
|
||||||
|
|
Loading…
Reference in New Issue