|
@@ -0,0 +1,79 @@
|
|
|
+package service
|
|
|
+
|
|
|
+import (
|
|
|
+ "context"
|
|
|
+ "encoding/json"
|
|
|
+ v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
|
|
|
+ "github.com/go-nunu/nunu-layout-advanced/internal/repository"
|
|
|
+)
|
|
|
+
|
|
|
+type WebsocketService interface {
|
|
|
+ AddWebsocket(ctx context.Context) (int64, error)
|
|
|
+ EnableOrDisable(ctx context.Context,serverId int64,websocketId int64,isOn bool,isPrior bool) error
|
|
|
+}
|
|
|
+func NewWebsocketService(
|
|
|
+ service *Service,
|
|
|
+ cdn CdnService,
|
|
|
+ webForwardingRep repository.WebForwardingRepository,
|
|
|
+) WebsocketService {
|
|
|
+ return &websocketService{
|
|
|
+ Service: service,
|
|
|
+ cdn: cdn,
|
|
|
+ webForwardingRep: webForwardingRep,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+type websocketService struct {
|
|
|
+ *Service
|
|
|
+ cdn CdnService
|
|
|
+ webForwardingRep repository.WebForwardingRepository
|
|
|
+}
|
|
|
+
|
|
|
+func (s *websocketService) AddWebsocket(ctx context.Context) (int64, error) {
|
|
|
+
|
|
|
+ handshakeTimeout := v1.HandshakeTimeoutJSON{
|
|
|
+ Unit: "second",
|
|
|
+ Count: 30,
|
|
|
+ }
|
|
|
+ handshakeTimeoutJSON, err := json.Marshal(handshakeTimeout)
|
|
|
+ if err != nil {
|
|
|
+ return 0, err
|
|
|
+ }
|
|
|
+ req := v1.WebSocket{
|
|
|
+ AllowAllOrigins: true,
|
|
|
+ RequestSameOrigin: true,
|
|
|
+ HandshakeTimeoutJSON: handshakeTimeoutJSON,
|
|
|
+ }
|
|
|
+
|
|
|
+ id, err := s.cdn.AddWebSockets(ctx, req)
|
|
|
+ if err != nil {
|
|
|
+ return 0, err
|
|
|
+ }
|
|
|
+ return id, nil
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func (s *websocketService) EnableOrDisable(ctx context.Context,serverId int64,websocketId int64,isOn bool,isPrior bool) error {
|
|
|
+ webId, err := s.webForwardingRep.GetWebId(ctx, serverId)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ type WebSocket struct {
|
|
|
+ IsPrior bool `json:"isPrior" form:"isPrior"`
|
|
|
+ IsOn bool `json:"isOn" form:"isOn"`
|
|
|
+ WebsocketId int64 `json:"websocketId" form:"websocketId"`
|
|
|
+ }
|
|
|
+ websocket := WebSocket{
|
|
|
+ IsPrior: isPrior,
|
|
|
+ IsOn: isOn,
|
|
|
+ WebsocketId: websocketId,
|
|
|
+ }
|
|
|
+ websocketJSON, err := json.Marshal(websocket)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if err := s.cdn.EditHTTPWebWebsocket(ctx, webId, websocketJSON); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|