add db connection, refactor code, add db migration files
This commit is contained in:
59
internal/foundry/requests/config.go
Normal file
59
internal/foundry/requests/config.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package requests
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrorSidWrongFormat = errors.New("Session id wrong format")
|
||||
ErrorSidNotFound = errors.New("Session id didn't find in response header")
|
||||
)
|
||||
|
||||
const (
|
||||
AuthPath = "/auth"
|
||||
LicensePath = "/license"
|
||||
JoinPath = "/join"
|
||||
PlayersPath = "/players"
|
||||
SetupPath = "/setup"
|
||||
UpdatePath = "/update"
|
||||
|
||||
SocketPath = "/socket.io"
|
||||
)
|
||||
|
||||
var WsTypeData = map[string]string{
|
||||
AuthPath: "getAuthData",
|
||||
LicensePath: "getAuthData",
|
||||
JoinPath: "getJoinData",
|
||||
PlayersPath: "getPlayersData",
|
||||
SetupPath: "getSetupData",
|
||||
UpdatePath: "getUpdateData",
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Host string
|
||||
Password string
|
||||
SessionID string
|
||||
}
|
||||
|
||||
func (conf *Config) SetSessionTokenFromHeader(resp http.Header) (bool, error) {
|
||||
setCookieHeader := resp.Get("Set-Cookie")
|
||||
if setCookieHeader == "" {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
for value := range strings.SplitSeq(setCookieHeader, ";") {
|
||||
if strings.Contains(value, "session") {
|
||||
sessionCookie := strings.Split(value, "=")
|
||||
if len(sessionCookie) != 2 {
|
||||
return false, ErrorSidWrongFormat
|
||||
}
|
||||
|
||||
conf.SessionID = sessionCookie[1]
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
64
internal/foundry/requests/get_requests.go
Normal file
64
internal/foundry/requests/get_requests.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package requests
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/models"
|
||||
)
|
||||
|
||||
func (conf *Config) GetSessionId() error {
|
||||
getResp, err := http.Get(fmt.Sprintf("http://%s%s", conf.Host, AuthPath))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = conf.SetSessionTokenFromHeader(getResp.Header)
|
||||
return err
|
||||
}
|
||||
|
||||
func (conf *Config) GetSessionToken() (bool, error) {
|
||||
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s%s", conf.Host, AuthPath), nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
header := http.Header{}
|
||||
header.Set("Cookie", fmt.Sprintf("session=%s", conf.SessionID))
|
||||
header.Set("Connection", "keep-alive")
|
||||
header.Set("Host", conf.Host)
|
||||
header.Set("Origin", fmt.Sprintf("http://%s", conf.Host))
|
||||
header.Set("Referer", fmt.Sprintf("http://%s%s", conf.Host, AuthPath))
|
||||
|
||||
req.Header = header
|
||||
client := &http.Client{}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return conf.SetSessionTokenFromHeader(resp.Header)
|
||||
}
|
||||
|
||||
func (conf *Config) GetStatus() (*models.Status, error) {
|
||||
getResp, err := http.Get(fmt.Sprintf("http://%s/api/status", conf.Host))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer getResp.Body.Close()
|
||||
|
||||
statusByte := make([]byte, 64)
|
||||
_, err = getResp.Body.Read(statusByte)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var status models.Status
|
||||
err = json.Unmarshal(statusByte, &status)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &status, nil
|
||||
}
|
||||
38
internal/foundry/requests/post_requests.go
Normal file
38
internal/foundry/requests/post_requests.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package requests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func (conf *Config) PostAuthenticationData() (*http.Response, error) {
|
||||
authData := url.Values{}
|
||||
authData.Add("adminPassword", conf.Password)
|
||||
authData.Add("action", "adminAuth")
|
||||
|
||||
req, err := http.NewRequest("POST", fmt.Sprintf("http://%s%s", conf.Host, AuthPath), bytes.NewBuffer([]byte(authData.Encode())))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
header := http.Header{}
|
||||
header.Set("Cookie", fmt.Sprintf("session=%s", conf.SessionID))
|
||||
header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
|
||||
header.Set("Connection", "keep-alive")
|
||||
header.Set("Content-Length", fmt.Sprintf("%d", len(authData.Encode())))
|
||||
header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
header.Set("Host", conf.Host)
|
||||
header.Set("Origin", fmt.Sprintf("http://%s", conf.Host))
|
||||
header.Set("Referer", fmt.Sprintf("http://%s%s", conf.Host, AuthPath))
|
||||
|
||||
req.Header = header
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user