Skip to content

Commit fc19771

Browse files
committed
updated requests to optionally use the stock dedicated server API at :7777
1 parent 725dc8c commit fc19771

18 files changed

+100
-20
lines changed

Companion/exporter/collector_runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func NewCollectorRunner(ctx context.Context, frmBaseUrl string, collectors ...Co
4444

4545
func (c *CollectorRunner) updateSessionName() {
4646
details := SessionInfo{}
47-
err := retrieveData(c.frmBaseUrl+"/getSessionInfo", &details)
47+
err := retrieveData(c.frmBaseUrl, "/getSessionInfo", &details)
4848
if err != nil {
4949
log.Printf("error reading session name from FRM: %s\n", err)
5050
return

Companion/exporter/common.go

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
package exporter
22

33
import (
4+
"bytes"
5+
"crypto/tls"
46
"encoding/json"
57
"fmt"
68
"github.com/coder/quartz"
79
"log"
810
"net/http"
11+
"net/url"
912
"regexp"
1013
"strings"
1114
"time"
1215
)
1316

17+
// Struct for the JSON body of POST requests
18+
type FrmApiRequest struct {
19+
Function string `json:"function"`
20+
Endpoint string `json:"endpoint"`
21+
}
22+
23+
// Reusable HTTP client for HTTPS w/ self-signed certs
24+
var tlsClient = &http.Client{
25+
Timeout: 10 * time.Second,
26+
Transport: &http.Transport{
27+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
28+
},
29+
}
30+
1431
var timeRegex = regexp.MustCompile(`\d\d:\d\d:\d\d`)
1532

1633
var Clock = quartz.NewReal()
@@ -35,7 +52,7 @@ func parseBool(b bool) float64 {
3552
}
3653
}
3754

38-
func retrieveData(frmAddress string, details any) error {
55+
func retrieveDataViaGET(frmAddress string, details any) error {
3956
resp, err := http.Get(frmAddress)
4057

4158
if err != nil {
@@ -53,3 +70,58 @@ func retrieveData(frmAddress string, details any) error {
5370
err = decoder.Decode(&details)
5471
return err
5572
}
73+
74+
func retrieveDataViaPOST(frmApiUrl string, endpointName string, details any) error {
75+
reqBody := FrmApiRequest{
76+
Function: "frm",
77+
Endpoint: endpointName,
78+
}
79+
jsonData, err := json.Marshal(reqBody)
80+
if err != nil {
81+
log.Printf("error marshalling request body: %s\n", err)
82+
return err
83+
}
84+
85+
req, err := http.NewRequest("POST", frmApiUrl, bytes.NewBuffer(jsonData))
86+
if err != nil {
87+
log.Printf("error creating POST request: %s\n", err)
88+
return err
89+
}
90+
req.Header.Set("Content-Type", "application/json")
91+
92+
resp, err := tlsClient.Do(req)
93+
if err != nil {
94+
log.Printf("error fetching statistics from FRM: %s\n", err)
95+
return err
96+
}
97+
defer resp.Body.Close()
98+
99+
if resp.StatusCode != 200 {
100+
return fmt.Errorf("non-200 returned when retrieving data: %d", resp.StatusCode)
101+
}
102+
103+
decoder := json.NewDecoder(resp.Body)
104+
err = decoder.Decode(&details)
105+
return err
106+
}
107+
108+
func retrieveData(frmAddress string, endpoint string, details any) error {
109+
u, err := url.Parse(frmAddress)
110+
if err != nil {
111+
return fmt.Errorf("invalid FRM address URL: %s", err)
112+
}
113+
114+
// Check if we're using the Dedicated Server API (1.1)
115+
if strings.HasSuffix(u.Path, "/api/v1") {
116+
// Dedicated server mode.
117+
// frmAddress is "https://host:7777/api/v1"
118+
// endpoint is "/getPower", so we strip the slash
119+
endpointName := strings.TrimPrefix(endpoint, "/")
120+
return retrieveDataViaPOST(frmAddress, endpointName, details)
121+
} else {
122+
// Web server mode.
123+
// frmAddress is "http://host:8080"
124+
// endpoint is "/getPower"
125+
return retrieveDataViaGET(frmAddress+endpoint, details)
126+
}
127+
}

Companion/exporter/drone_station_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func NewDroneStationCollector(endpoint string) *DroneStationCollector {
6767

6868
func (c *DroneStationCollector) Collect(frmAddress string, sessionName string) {
6969
details := []DroneStationDetails{}
70-
err := retrieveData(frmAddress+c.endpoint, &details)
70+
err := retrieveData(frmAddress, c.endpoint, &details)
7171
if err != nil {
7272
c.metricsDropper.DropStaleMetricLabels()
7373
log.Printf("error reading drone station statistics from FRM: %s\n", err)

Companion/exporter/extractor_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func NewExtractorCollector(endpoint string) *ExtractorCollector {
4040

4141
func (c *ExtractorCollector) Collect(frmAddress string, sessionName string) {
4242
details := []ExtractorDetails{}
43-
err := retrieveData(frmAddress+c.endpoint, &details)
43+
err := retrieveData(frmAddress, c.endpoint, &details)
4444
if err != nil {
4545
log.Printf("error reading extractor statistics from FRM: %s\n", err)
4646
return

Companion/exporter/factory_building_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NewFactoryBuildingCollector(endpoint string) *FactoryBuildingCollector {
2424

2525
func (c *FactoryBuildingCollector) Collect(frmAddress string, sessionName string) {
2626
details := []BuildingDetail{}
27-
err := retrieveData(frmAddress+c.endpoint, &details)
27+
err := retrieveData(frmAddress, c.endpoint, &details)
2828
if err != nil {
2929
c.metricsDropper.DropStaleMetricLabels()
3030
log.Printf("error reading factory buildings from FRM: %s\n", err)

Companion/exporter/fracking_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func NewFrackingCollector(endpoint string) *FrackingCollector {
4040

4141
func (c *FrackingCollector) Collect(frmAddress string, sessionName string) {
4242
details := []FrackingDetails{}
43-
err := retrieveData(frmAddress+c.endpoint, &details)
43+
err := retrieveData(frmAddress, c.endpoint, &details)
4444
if err != nil {
4545
log.Printf("error reading fracking statistics from FRM: %s\n", err)
4646
return

Companion/exporter/hypertube_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func NewHypertubeCollector(endpoint string) *HypertubeCollector {
4040

4141
func (c *HypertubeCollector) Collect(frmAddress string, sessionName string) {
4242
details := []HypertubeDetails{}
43-
err := retrieveData(frmAddress+c.endpoint, &details)
43+
err := retrieveData(frmAddress, c.endpoint, &details)
4444
if err != nil {
4545
log.Printf("error reading hypertube statistics from FRM: %s\n", err)
4646
return

Companion/exporter/portal_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func NewPortalCollector(endpoint string) *PortalCollector {
4040

4141
func (c *PortalCollector) Collect(frmAddress string, sessionName string) {
4242
details := []PortalDetails{}
43-
err := retrieveData(frmAddress+c.endpoint, &details)
43+
err := retrieveData(frmAddress, c.endpoint, &details)
4444
if err != nil {
4545
log.Printf("error reading portal statistics from FRM: %s\n", err)
4646
return

Companion/exporter/power_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func NewPowerCollector(endpoint string) *PowerCollector {
114114

115115
func (c *PowerCollector) Collect(frmAddress string, sessionName string) {
116116
details := []PowerDetails{}
117-
err := retrieveData(frmAddress+c.endpoint, &details)
117+
err := retrieveData(frmAddress, c.endpoint, &details)
118118
if err != nil {
119119
c.metricsDropper.DropStaleMetricLabels()
120120
log.Printf("error reading power statistics from FRM: %s\n", err)

Companion/exporter/production_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func NewProductionCollector(endpoint string) *ProductionCollector {
3737

3838
func (c *ProductionCollector) Collect(frmAddress string, sessionName string) {
3939
details := []ProductionDetails{}
40-
err := retrieveData(frmAddress+c.endpoint, &details)
40+
err := retrieveData(frmAddress, c.endpoint, &details)
4141
if err != nil {
4242
c.metricsDropper.DropStaleMetricLabels()
4343
log.Printf("error reading production statistics from FRM: %s\n", err)

0 commit comments

Comments
 (0)