2024-07-14 12:25:30 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2024-08-23 10:50:14 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/internal/directory"
|
2024-07-14 12:25:30 +00:00
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/internal/models"
|
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/internal/services"
|
2024-08-23 10:50:14 +00:00
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
2024-07-14 12:25:30 +00:00
|
|
|
"github.com/gofiber/contrib/websocket"
|
2024-08-23 10:50:14 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2024-07-14 12:25:30 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-08-23 10:50:14 +00:00
|
|
|
"github.com/spf13/viper"
|
2024-07-14 12:25:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func listenWebsocket(c *websocket.Conn) {
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
|
|
|
|
// Push connection
|
|
|
|
services.ClientRegister(user, c)
|
|
|
|
log.Debug().Uint("user", user.ID).Msg("New websocket connection established...")
|
|
|
|
|
|
|
|
// Event loop
|
2024-08-23 10:50:14 +00:00
|
|
|
var mt int
|
|
|
|
var data []byte
|
2024-07-14 12:25:30 +00:00
|
|
|
var err error
|
|
|
|
|
2024-08-23 10:50:14 +00:00
|
|
|
var packet hyper.NetworkPackage
|
|
|
|
|
2024-07-14 12:25:30 +00:00
|
|
|
for {
|
2024-08-23 10:50:14 +00:00
|
|
|
if mt, data, err = c.ReadMessage(); err != nil {
|
2024-07-14 12:25:30 +00:00
|
|
|
break
|
2024-08-23 10:50:14 +00:00
|
|
|
} else if err := jsoniter.Unmarshal(data, &packet); err != nil {
|
|
|
|
_ = c.WriteMessage(mt, hyper.NetworkPackage{
|
|
|
|
Action: "error",
|
|
|
|
Message: "unable to unmarshal your command, requires json request",
|
|
|
|
}.Marshal())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
aliasingMap := viper.GetStringMapString("services.aliases")
|
|
|
|
if val, ok := aliasingMap[packet.Endpoint]; ok {
|
|
|
|
packet.Endpoint = val
|
|
|
|
}
|
|
|
|
|
|
|
|
service := directory.GetServiceInstanceByType(packet.Endpoint)
|
|
|
|
if service == nil {
|
|
|
|
_ = c.WriteMessage(mt, hyper.NetworkPackage{
|
|
|
|
Action: "error",
|
|
|
|
Message: "service not found",
|
|
|
|
}.Marshal())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pc, err := service.GetGrpcConn()
|
|
|
|
if err != nil {
|
|
|
|
_ = c.WriteMessage(mt, hyper.NetworkPackage{
|
|
|
|
Action: "error",
|
|
|
|
Message: fmt.Sprintf("unable to connect to service: %v", err.Error()),
|
|
|
|
}.Marshal())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
sc := proto.NewStreamControllerClient(pc)
|
|
|
|
_, err = sc.EmitStreamEvent(context.Background(), &proto.StreamEventRequest{
|
|
|
|
Event: packet.Action,
|
|
|
|
UserId: uint64(user.ID),
|
|
|
|
Payload: packet.RawPayload(),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
_ = c.WriteMessage(mt, hyper.NetworkPackage{
|
|
|
|
Action: "error",
|
|
|
|
Message: fmt.Sprintf("unable send message to service: %v", err.Error()),
|
|
|
|
}.Marshal())
|
|
|
|
continue
|
2024-07-14 12:25:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pop connection
|
|
|
|
services.ClientUnregister(user, c)
|
|
|
|
log.Debug().Uint("user", user.ID).Msg("A websocket connection disconnected...")
|
|
|
|
}
|