add db connection, refactor code, add db migration files
This commit is contained in:
@@ -3,26 +3,37 @@ package foundry
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"log/slog"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/requests"
|
||||
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/types"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type TransportCode int
|
||||
|
||||
const (
|
||||
WriterCode = TransportCode(0)
|
||||
ReaderCode = TransportCode(1)
|
||||
)
|
||||
|
||||
type webSocketUtil struct {
|
||||
wsConn *websocket.Conn
|
||||
currWsId int
|
||||
isReadReady bool
|
||||
logger *slog.Logger
|
||||
|
||||
msgMap map[int](chan []byte)
|
||||
msgMap map[int](chan []byte)
|
||||
chanMutex sync.Mutex
|
||||
channels types.Channels
|
||||
}
|
||||
|
||||
mutex sync.Mutex
|
||||
channels Channels
|
||||
func NewWebSocketUtil() *webSocketUtil {
|
||||
return &webSocketUtil{currWsId: 0, isReadReady: false, msgMap: make(map[int]chan []byte)}
|
||||
}
|
||||
|
||||
func parseCode(msg *string) string {
|
||||
@@ -54,53 +65,28 @@ func parseId(msg *string, start int) (int, int) {
|
||||
return msgId, j
|
||||
}
|
||||
|
||||
func parseWsRespMessage(msg string) (*wsMessage, error) {
|
||||
data := &wsMessage{}
|
||||
func parseWsRespMessage(msg string) (*types.WsMessage, error) {
|
||||
data := &types.WsMessage{}
|
||||
|
||||
data.code = parseCode(&msg)
|
||||
if data.code != RespDataCode {
|
||||
data.Code = parseCode(&msg)
|
||||
if data.Code != RespDataCode {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
i := len(data.code)
|
||||
i := len(data.Code)
|
||||
if i == 0 {
|
||||
return nil, ErrorMsgNotHaveNumber
|
||||
}
|
||||
|
||||
data.id, i = parseId(&msg, i)
|
||||
data.Id, i = parseId(&msg, i)
|
||||
if i == 0 {
|
||||
return nil, ErrorMsgNotHaveNumber
|
||||
}
|
||||
|
||||
data.msgJson = msg[i:]
|
||||
data.MsgJson = msg[i:]
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func ConnectToWebSocket(foundry *Foundry) error {
|
||||
if !foundry.isAuth {
|
||||
return ErrorNotAuth
|
||||
}
|
||||
|
||||
query := url.Values{}
|
||||
query.Add("session", foundry.sessionID)
|
||||
query.Add("EIO", "4")
|
||||
query.Add("transport", "websocket")
|
||||
|
||||
u := url.URL{Scheme: "ws", Host: foundry.config.host, Path: fmt.Sprintf("%s/", socketPath), RawQuery: query.Encode()}
|
||||
|
||||
wsHeader := http.Header{}
|
||||
wsHeader.Set("Cookie", fmt.Sprintf("session=%s", foundry.sessionID))
|
||||
|
||||
wsConn, _, err := websocket.DefaultDialer.Dial(u.String(), wsHeader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
foundry.ws.wsConn = wsConn
|
||||
foundry.ws.currWsId = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: Lookup timeout
|
||||
func (ws *webSocketUtil) ReceiveMessage(id int) ([]byte, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
@@ -130,8 +116,8 @@ func (ws *webSocketUtil) ReceiveMessage(id int) ([]byte, error) {
|
||||
func (ws *webSocketUtil) closeMsgChannel(id int, timeout time.Duration) bool {
|
||||
time.Sleep(timeout)
|
||||
|
||||
ws.mutex.Lock()
|
||||
defer ws.mutex.Unlock()
|
||||
ws.chanMutex.Lock()
|
||||
defer ws.chanMutex.Unlock()
|
||||
|
||||
ok := true
|
||||
if _, ok = ws.msgMap[id]; !ok {
|
||||
@@ -147,21 +133,22 @@ func (ws *webSocketUtil) closeMsgChannel(id int, timeout time.Duration) bool {
|
||||
close(ws.msgMap[id])
|
||||
delete(ws.msgMap, id)
|
||||
}
|
||||
fmt.Printf("Channel has been closed(id=%d, timeout=%d)\n", id, timeout)
|
||||
|
||||
ws.logger.Debug("WS: Channel has been closed\n", "id", id, "timeout", timeout.String())
|
||||
return ok
|
||||
}
|
||||
|
||||
func (ws *webSocketUtil) HandleWebsocketRequest(msg *wsMessage) ([]byte, error) {
|
||||
func (ws *webSocketUtil) HandleWebsocketRequest(msg *types.WsMessage) ([]byte, error) {
|
||||
if !ws.IsReadReady() {
|
||||
return nil, ErrorIsNotReady
|
||||
}
|
||||
|
||||
err := ws.wsConn.WriteMessage(websocket.TextMessage, msg.toByteSlice())
|
||||
err := ws.wsConn.WriteMessage(websocket.TextMessage, msg.ToByteSlice())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data, err := ws.ReceiveMessage(msg.id)
|
||||
data, err := ws.ReceiveMessage(msg.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -169,10 +156,9 @@ func (ws *webSocketUtil) HandleWebsocketRequest(msg *wsMessage) ([]byte, error)
|
||||
}
|
||||
|
||||
func (ws *webSocketUtil) sendOnlyCodeRequest(code string) error {
|
||||
msgToSend := []byte(CodesRespToReq[code])
|
||||
log.Printf("send: %s\n", msgToSend)
|
||||
ws.logger.Debug("WS: Data has been send\n", "msg", CodesRespToReq[code])
|
||||
|
||||
return ws.wsConn.WriteMessage(websocket.TextMessage, msgToSend)
|
||||
return ws.wsConn.WriteMessage(websocket.TextMessage, []byte(CodesRespToReq[code]))
|
||||
}
|
||||
|
||||
func (ws *webSocketUtil) ListenWebSocket() {
|
||||
@@ -195,22 +181,22 @@ func (ws *webSocketUtil) ServeWebSocket() {
|
||||
for {
|
||||
select {
|
||||
case message := <-ws.channels.Msg():
|
||||
log.Println("recv code:", message.code)
|
||||
ws.logger.Debug("WS: Data has been received\n", "msgCode", message.Code)
|
||||
|
||||
switch message.code {
|
||||
switch message.Code {
|
||||
case RespPingCode, RespSessionData:
|
||||
err := ws.sendOnlyCodeRequest(message.code)
|
||||
err := ws.sendOnlyCodeRequest(message.Code)
|
||||
if err != nil {
|
||||
ws.isReadReady = false
|
||||
ws.channels.err <- &FoundryError{Type: WriterCode, Err: err}
|
||||
ws.channels.Err() <- &FoundryError{Type: WriterCode, Err: err}
|
||||
continue
|
||||
}
|
||||
case RespCreateSessionCode:
|
||||
ws.isReadReady = true
|
||||
case RespDataCode:
|
||||
ws.msgMap[message.id] = make(chan []byte, 1)
|
||||
ws.msgMap[message.id] <- []byte(message.msgJson)
|
||||
go ws.closeMsgChannel(message.id, 5*time.Second)
|
||||
ws.msgMap[message.Id] = make(chan []byte, 1)
|
||||
ws.msgMap[message.Id] <- []byte(message.MsgJson)
|
||||
go ws.closeMsgChannel(message.Id, 5*time.Second)
|
||||
default:
|
||||
}
|
||||
case <-ws.channels.Done():
|
||||
@@ -219,18 +205,14 @@ func (ws *webSocketUtil) ServeWebSocket() {
|
||||
}
|
||||
}
|
||||
|
||||
func (ws *webSocketUtil) InitWsChannels() *Channels {
|
||||
ws.channels.done = make(chan struct{})
|
||||
ws.channels.err = make(chan error, 10)
|
||||
ws.channels.msg = make(chan *wsMessage, 10)
|
||||
|
||||
return &ws.channels
|
||||
}
|
||||
|
||||
func NewWebSocketUtil() *webSocketUtil {
|
||||
return &webSocketUtil{currWsId: 0, isReadReady: false, msgMap: make(map[int]chan []byte), channels: Channels{}}
|
||||
}
|
||||
|
||||
func (ws *webSocketUtil) IsReadReady() bool {
|
||||
return ws.isReadReady
|
||||
}
|
||||
|
||||
func (ws *webSocketUtil) CreateWSMessageByPage(page string) *types.WsMessage {
|
||||
msgToSend := &types.WsMessage{Code: CodesRespToReq[RespCreateSessionCode], Id: ws.currWsId, MsgJson: fmt.Sprintf("[\"%s\"]", requests.WsTypeData[page])}
|
||||
ws.currWsId++
|
||||
|
||||
ws.logger.Debug("WS: Data to send\n", "msgToSend", msgToSend.ToString())
|
||||
return msgToSend
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user