Spaces:
Runtime error
Runtime error
File size: 1,891 Bytes
857a91b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | // Package controlplane holds the (mostly designed, partly stubbed) outbound
// connection from matrix-runtime to MatrixHub Cloud. In the hybrid model the
// runtime lives in customer infrastructure and dials out to the cloud control
// plane, so no inbound firewall exposure is required.
package controlplane
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
)
// Client talks to MatrixHub Cloud over outbound HTTPS.
type Client struct {
CloudURL string
JoinToken string
RuntimeID string
Workspace string
HTTP *http.Client
}
// New builds a control-plane client.
func New(cloudURL, joinToken, runtimeID, workspace string) *Client {
return &Client{
CloudURL: strings.TrimRight(cloudURL, "/"),
JoinToken: joinToken,
RuntimeID: runtimeID,
Workspace: workspace,
HTTP: &http.Client{Timeout: 30 * time.Second},
}
}
// Heartbeat reports liveness to the cloud. Stubbed: it performs a single
// authenticated POST to /v1/runtimes/heartbeat and tolerates absence of the
// endpoint, since the MVP runs primarily via the direct HTTP API.
func (c *Client) Heartbeat(ctx context.Context, capabilities any) error {
if c.CloudURL == "" || c.JoinToken == "" {
return fmt.Errorf("control plane not configured")
}
body, _ := json.Marshal(map[string]any{
"runtime_id": c.RuntimeID,
"workspace": c.Workspace,
"capabilities": capabilities,
})
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.CloudURL+"/v1/runtimes/heartbeat", strings.NewReader(string(body)))
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+c.JoinToken)
req.Header.Set("Content-Type", "application/json")
resp, err := c.HTTP.Do(req)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode >= 400 {
return fmt.Errorf("heartbeat rejected: status %d", resp.StatusCode)
}
return nil
}
|