mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
135 lines
3.8 KiB
Go
135 lines
3.8 KiB
Go
package sandbox
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Client is the panel-side HTTP client for artex-runner.
|
|
type Client struct {
|
|
baseURL string
|
|
token string
|
|
http *http.Client
|
|
}
|
|
|
|
// NewClient constructs a runner client. Ambient HTTP_PROXY is explicitly disabled
|
|
// (Proxy: nil) so panel->runner calls on runner-net are never proxied.
|
|
func NewClient(baseURL, token string) *Client {
|
|
tr := &http.Transport{
|
|
Proxy: nil,
|
|
MaxIdleConns: 16,
|
|
IdleConnTimeout: 60 * time.Second,
|
|
ResponseHeaderTimeout: 0, // exec calls can run up to MaxTimeoutMS
|
|
}
|
|
return &Client{
|
|
baseURL: strings.TrimRight(strings.TrimSpace(baseURL), "/"),
|
|
token: strings.TrimSpace(token),
|
|
http: &http.Client{Transport: tr},
|
|
}
|
|
}
|
|
|
|
func (c *Client) doJSON(ctx context.Context, method, path string, reqBody, out any) error {
|
|
if c == nil || c.baseURL == "" {
|
|
return fmt.Errorf("%w: runner URL is empty", ErrRunnerUnavailable)
|
|
}
|
|
var body io.Reader
|
|
if reqBody != nil {
|
|
raw, err := json.Marshal(reqBody)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
body = bytes.NewReader(raw)
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if reqBody != nil {
|
|
req.Header.Set("Content-Type", "application/json")
|
|
}
|
|
if c.token != "" {
|
|
req.Header.Set("Authorization", "Bearer "+c.token)
|
|
}
|
|
resp, err := c.http.Do(req)
|
|
if err != nil {
|
|
if ctx.Err() != nil {
|
|
return fmt.Errorf("runner request canceled: %w", ctx.Err())
|
|
}
|
|
return fmt.Errorf("%w: %v", ErrRunnerUnavailable, err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// Cap error/JSON reads to 4 MiB (ExecResponse carries <= 2 MiB of stdout+stderr + JSON overhead).
|
|
respRaw, err := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
|
|
if err != nil {
|
|
if ctx.Err() != nil {
|
|
return fmt.Errorf("runner response canceled: %w", ctx.Err())
|
|
}
|
|
return fmt.Errorf("%w: read response: %v", ErrRunnerUnavailable, err)
|
|
}
|
|
if resp.StatusCode >= 400 {
|
|
msg := strings.TrimSpace(string(respRaw))
|
|
switch resp.StatusCode {
|
|
case http.StatusBadRequest:
|
|
return fmt.Errorf("%w: %s", ErrBadRequest, msg)
|
|
case http.StatusUnauthorized:
|
|
return fmt.Errorf("%w: %s", ErrUnauthorized, msg)
|
|
case http.StatusNotFound:
|
|
return fmt.Errorf("%w: %s", ErrNotFound, msg)
|
|
case http.StatusConflict:
|
|
return fmt.Errorf("%w: %s", ErrDockerUnavailable, msg)
|
|
default:
|
|
return fmt.Errorf("%w: HTTP %d: %s", ErrRunnerUnavailable, resp.StatusCode, msg)
|
|
}
|
|
}
|
|
if out != nil && len(respRaw) > 0 {
|
|
if err := json.Unmarshal(respRaw, out); err != nil {
|
|
return fmt.Errorf("decode runner response: %w", err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Health calls GET /v1/health.
|
|
func (c *Client) Health(ctx context.Context) (HealthResponse, error) {
|
|
var out HealthResponse
|
|
err := c.doJSON(ctx, http.MethodGet, "/v1/health", nil, &out)
|
|
return out, err
|
|
}
|
|
|
|
// Exec calls POST /v1/tasks/{id}/exec.
|
|
func (c *Client) Exec(ctx context.Context, taskID string, req ExecRequest) (ExecResponse, error) {
|
|
if err := ValidateTaskID(taskID); err != nil {
|
|
return ExecResponse{}, err
|
|
}
|
|
var out ExecResponse
|
|
err := c.doJSON(ctx, http.MethodPost, "/v1/tasks/"+url.PathEscape(taskID)+"/exec", req, &out)
|
|
return out, err
|
|
}
|
|
|
|
// Kill calls POST /v1/tasks/{id}/kill.
|
|
func (c *Client) Kill(ctx context.Context, taskID string) (KillResponse, error) {
|
|
if err := ValidateTaskID(taskID); err != nil {
|
|
return KillResponse{}, err
|
|
}
|
|
var out KillResponse
|
|
err := c.doJSON(ctx, http.MethodPost, "/v1/tasks/"+url.PathEscape(taskID)+"/kill", nil, &out)
|
|
return out, err
|
|
}
|
|
|
|
// Delete calls DELETE /v1/tasks/{id}.
|
|
func (c *Client) Delete(ctx context.Context, taskID string) (DeleteResponse, error) {
|
|
if err := ValidateTaskID(taskID); err != nil {
|
|
return DeleteResponse{}, err
|
|
}
|
|
var out DeleteResponse
|
|
err := c.doJSON(ctx, http.MethodDelete, "/v1/tasks/"+url.PathEscape(taskID), nil, &out)
|
|
return out, err
|
|
}
|