2023-10-03 22:53:47 +08:00
|
|
|
package websrv
|
2023-09-30 07:05:23 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2024-02-13 04:11:30 +08:00
|
|
|
"github.com/iMDT/bbb-graphql-middleware/internal/common"
|
2023-10-03 22:53:47 +08:00
|
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2023-11-30 19:58:37 +08:00
|
|
|
"os"
|
2023-10-03 22:53:47 +08:00
|
|
|
"strings"
|
2023-09-30 07:05:23 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-10-03 22:53:47 +08:00
|
|
|
var redisClient = redis.NewClient(&redis.Options{
|
2023-11-30 19:58:37 +08:00
|
|
|
Addr: os.Getenv("BBB_GRAPHQL_MIDDLEWARE_REDIS_ADDRESS"),
|
|
|
|
Password: os.Getenv("BBB_GRAPHQL_MIDDLEWARE_REDIS_PASSWORD"),
|
2023-10-03 22:53:47 +08:00
|
|
|
DB: 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
func GetRedisConn() *redis.Client {
|
|
|
|
return redisClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func StartRedisListener() {
|
|
|
|
log := log.WithField("_routine", "StartRedisListener")
|
|
|
|
|
|
|
|
var ctx = context.Background()
|
|
|
|
|
|
|
|
subscriber := GetRedisConn().Subscribe(ctx, "from-akka-apps-redis-channel")
|
|
|
|
|
|
|
|
for {
|
|
|
|
msg, err := subscriber.ReceiveMessage(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip parsing unnecessary messages
|
2024-02-15 00:28:19 +08:00
|
|
|
if !strings.Contains(msg.Payload, "ForceUserGraphqlReconnectionSysMsg") &&
|
|
|
|
!strings.Contains(msg.Payload, "CheckGraphqlMiddlewareAlivePingSysMsg") {
|
2023-10-03 22:53:47 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var message interface{}
|
|
|
|
if err := json.Unmarshal([]byte(msg.Payload), &message); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
messageAsMap := message.(map[string]interface{})
|
|
|
|
|
|
|
|
messageEnvelopeAsMap := messageAsMap["envelope"].(map[string]interface{})
|
|
|
|
|
|
|
|
messageType := messageEnvelopeAsMap["name"]
|
|
|
|
|
2023-12-15 01:01:47 +08:00
|
|
|
if messageType == "ForceUserGraphqlReconnectionSysMsg" {
|
2023-10-03 22:53:47 +08:00
|
|
|
messageCoreAsMap := messageAsMap["core"].(map[string]interface{})
|
|
|
|
messageBodyAsMap := messageCoreAsMap["body"].(map[string]interface{})
|
|
|
|
sessionTokenToInvalidate := messageBodyAsMap["sessionToken"]
|
|
|
|
log.Debugf("Received invalidate request for sessionToken %v", sessionTokenToInvalidate)
|
|
|
|
|
|
|
|
//Not being used yet
|
2024-02-01 22:50:29 +08:00
|
|
|
go InvalidateSessionTokenConnections(sessionTokenToInvalidate.(string))
|
2023-10-03 22:53:47 +08:00
|
|
|
}
|
2024-02-15 00:28:19 +08:00
|
|
|
|
|
|
|
//Ping message requires a response with a Pong message
|
|
|
|
if messageType == "CheckGraphqlMiddlewareAlivePingSysMsg" &&
|
|
|
|
strings.Contains(msg.Payload, common.GetUniqueID()) {
|
|
|
|
messageCoreAsMap := messageAsMap["core"].(map[string]interface{})
|
|
|
|
messageBodyAsMap := messageCoreAsMap["body"].(map[string]interface{})
|
|
|
|
middlewareUID := messageBodyAsMap["middlewareUID"]
|
|
|
|
if middlewareUID == common.GetUniqueID() {
|
|
|
|
log.Debugf("Received ping message from akka-apps")
|
|
|
|
go SendCheckGraphqlMiddlewareAlivePongSysMsg()
|
|
|
|
}
|
|
|
|
}
|
2023-10-03 22:53:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-30 07:05:23 +08:00
|
|
|
func getCurrTimeInMs() int64 {
|
|
|
|
currentTime := time.Now()
|
|
|
|
milliseconds := currentTime.UnixNano() / int64(time.Millisecond)
|
|
|
|
return milliseconds
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendBbbCoreMsgToRedis(name string, body map[string]interface{}) {
|
|
|
|
channelName := "to-akka-apps-redis-channel"
|
|
|
|
|
|
|
|
message := map[string]interface{}{
|
|
|
|
"envelope": map[string]interface{}{
|
|
|
|
"name": name,
|
|
|
|
"routing": map[string]interface{}{
|
|
|
|
"sender": "bbb-graphql-middleware",
|
|
|
|
},
|
|
|
|
"timestamp": getCurrTimeInMs(),
|
|
|
|
},
|
|
|
|
"core": map[string]interface{}{
|
|
|
|
"header": map[string]interface{}{
|
|
|
|
"name": name,
|
|
|
|
},
|
|
|
|
"body": body,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
messageJSON, err := json.Marshal(message)
|
|
|
|
if err != nil {
|
2023-12-15 01:01:47 +08:00
|
|
|
log.Tracef("Error while marshaling message to json: %v\n", err)
|
2023-09-30 07:05:23 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = GetRedisConn().Publish(context.Background(), channelName, messageJSON).Err()
|
|
|
|
if err != nil {
|
2023-12-15 01:01:47 +08:00
|
|
|
log.Tracef("Error while sending msg to redis channel: %v\n", err)
|
2023-09-30 07:05:23 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-15 01:01:47 +08:00
|
|
|
log.Tracef("JSON message sent to channel %s:\n%s\n", channelName, messageJSON)
|
2023-09-30 07:05:23 +08:00
|
|
|
}
|
|
|
|
|
2023-12-15 01:01:47 +08:00
|
|
|
func SendUserGraphqlReconnectionForcedEvtMsg(sessionToken string) {
|
2023-09-30 07:05:23 +08:00
|
|
|
var body = map[string]interface{}{
|
2024-02-13 04:11:30 +08:00
|
|
|
"middlewareUID": common.GetUniqueID(),
|
|
|
|
"sessionToken": sessionToken,
|
2023-09-30 07:05:23 +08:00
|
|
|
}
|
|
|
|
|
2023-12-15 01:01:47 +08:00
|
|
|
sendBbbCoreMsgToRedis("UserGraphqlReconnectionForcedEvtMsg", body)
|
2023-09-30 07:05:23 +08:00
|
|
|
}
|
|
|
|
|
2023-12-15 01:01:47 +08:00
|
|
|
func SendUserGraphqlConnectionEstablishedSysMsg(sessionToken string, browserConnectionId string) {
|
2023-09-30 07:05:23 +08:00
|
|
|
var body = map[string]interface{}{
|
2024-02-13 04:11:30 +08:00
|
|
|
"middlewareUID": common.GetUniqueID(),
|
2023-09-30 07:05:23 +08:00
|
|
|
"sessionToken": sessionToken,
|
|
|
|
"browserConnectionId": browserConnectionId,
|
|
|
|
}
|
|
|
|
|
2023-12-15 01:01:47 +08:00
|
|
|
sendBbbCoreMsgToRedis("UserGraphqlConnectionEstablishedSysMsg", body)
|
2023-09-30 07:05:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func SendUserGraphqlConnectionClosedSysMsg(sessionToken string, browserConnectionId string) {
|
|
|
|
var body = map[string]interface{}{
|
2024-02-13 04:11:30 +08:00
|
|
|
"middlewareUID": common.GetUniqueID(),
|
2023-09-30 07:05:23 +08:00
|
|
|
"sessionToken": sessionToken,
|
|
|
|
"browserConnectionId": browserConnectionId,
|
|
|
|
}
|
|
|
|
|
|
|
|
sendBbbCoreMsgToRedis("UserGraphqlConnectionClosedSysMsg", body)
|
|
|
|
}
|
2024-02-15 00:28:19 +08:00
|
|
|
|
|
|
|
func SendCheckGraphqlMiddlewareAlivePongSysMsg() {
|
|
|
|
var body = map[string]interface{}{
|
|
|
|
"middlewareUID": common.GetUniqueID(),
|
|
|
|
}
|
|
|
|
|
|
|
|
sendBbbCoreMsgToRedis("CheckGraphqlMiddlewareAlivePongSysMsg", body)
|
|
|
|
}
|