65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
package foundry
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/models"
|
|
)
|
|
|
|
func (foundry *Foundry) setUpSessionId() error {
|
|
getResp, err := http.Get(fmt.Sprintf("http://%s%s", foundry.config.host, authPath))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = foundry.getSessionTokenFromHeader(getResp.Header)
|
|
return err
|
|
}
|
|
|
|
func (foundry *Foundry) CheckSessionToken(host string) (bool, error) {
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s%s", host, authPath), nil)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
header := http.Header{}
|
|
header.Set("Cookie", fmt.Sprintf("session=%s", foundry.sessionID))
|
|
header.Set("Connection", "keep-alive")
|
|
header.Set("Host", host)
|
|
header.Set("Origin", fmt.Sprintf("http://%s", host))
|
|
header.Set("Referer", fmt.Sprintf("http://%s%s", host, authPath))
|
|
|
|
req.Header = header
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return foundry.getSessionTokenFromHeader(resp.Header)
|
|
}
|
|
|
|
func (foundry *Foundry) GetStatus() (*models.Status, error) {
|
|
getResp, err := http.Get(fmt.Sprintf("http://%s/api/status", foundry.config.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
|
|
}
|