refactor foundry lib structure; add transport classes for db,http,websocket; add action interface instead of multiple function that accepts action classes; add changing foundry status on websocket responses
This commit is contained in:
101
internal/foundry/transport/http.go
Normal file
101
internal/foundry/transport/http.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (tr *FoundryTransport) LaunchWorld(worldName string) error {
|
||||
resp, err := tr.Http.PostLaunchWorld(worldName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
return errors.New("World is not found")
|
||||
}
|
||||
|
||||
tr.ExchangeChan.ProgressMsg[worldName] = make(chan struct{})
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
|
||||
select {
|
||||
case <-tr.ExchangeChan.ProgressMsg[worldName]:
|
||||
tr.Logger.Debug("World has been started(from GetInitialData)", "world", worldName)
|
||||
case <-ctx.Done():
|
||||
close(tr.ExchangeChan.ProgressMsg[worldName])
|
||||
err := ctx.Err()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ErrorTimeout
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tr *FoundryTransport) ConnectToFoundry() error {
|
||||
var err error
|
||||
|
||||
ok, err := tr.Http.GetSessionToken()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if tr.Http.Password != "" && !ok {
|
||||
err := tr.Authenticate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
tr.Logger.Info("Successefully connected to Foundry", "host", tr.Http.Host)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tr *FoundryTransport) Authenticate() error {
|
||||
if tr.Http.SessionID == "" {
|
||||
err := tr.Http.GetSessionId()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := tr.Http.PostAuthenticationData()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
contentLen, err := strconv.Atoi(resp.Header.Get("Content-Length"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tr.CheckAuthRespAnswer(resp.Body, contentLen)
|
||||
}
|
||||
|
||||
func (tr *FoundryTransport) CheckAuthRespAnswer(r io.Reader, respLength int) error {
|
||||
authBody := make([]byte, respLength)
|
||||
|
||||
_, err := r.Read(authBody)
|
||||
if err != nil && err != io.EOF {
|
||||
return err
|
||||
}
|
||||
|
||||
if !strings.Contains(string(authBody), "Admin authentication successful") {
|
||||
return ErrorAuthPassWrong
|
||||
}
|
||||
tr.IsAuth = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tr *FoundryTransport) HasSessionId() bool {
|
||||
return tr.Http.SessionID != ""
|
||||
}
|
||||
|
||||
func (tr *FoundryTransport) InitSessionId() error {
|
||||
return tr.Http.GetSessionId()
|
||||
}
|
||||
Reference in New Issue
Block a user