Files
nexus/backend/Controllers/GatewayHealthController.cs
T
developer 6ba788bb6e P2.1: Add GatewayConnector BackgroundService with WebSocket client
- GatewayConnector: BackgroundService maintaining persistent WebSocket connection
  to OpenClaw Gateway with Bearer token auth from backend env only
- IGatewayConnector: Interface exposing connection state, version info, health
- Exponential backoff reconnect: 1s initial, max 5min, with jitter
- Version pinning: RequiredVersion from config; fail-fast or warn on mismatch
- Pre-flight HTTP version check before WebSocket connect
- GatewayHealthController: GET /api/health/gateway with connection status
- GatewayConnectorOptions: Configurable via GatewayConnector config section
- Token never logged; read from Integrations:OpenClaw:Password/Token only
- 16 tests: options defaults, config binding, health endpoint responses,
  connection states, DI registration, routing attributes
2026-07-13 14:13:39 +02:00

34 lines
1.2 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Nexus.Api.Services;
namespace Nexus.Api.Controllers;
/// <summary>
/// Health-check endpoint for the Gateway WebSocket connection.
/// </summary>
[ApiController]
[AllowAnonymous]
public class GatewayHealthController(IGatewayConnector connector) : ControllerBase
{
/// <summary>
/// Returns the current Gateway WebSocket connection health.
/// </summary>
[HttpGet("/api/health/gateway")]
public IResult GetGatewayHealth()
{
return Results.Ok(new
{
status = connector.ConnectionState.ToString().ToLowerInvariant(),
connected = connector.ConnectionState == GatewayConnectionState.Connected,
gatewayVersion = connector.GatewayVersion ?? "unknown",
requiredVersion = connector.RequiredVersion,
versionPinned = connector.RequiredVersion is not null,
lastConnectedAt = connector.LastConnectedAt?.ToString("o"),
reconnectAttempts = connector.ReconnectAttempts,
message = connector.StatusMessage,
timestamp = DateTimeOffset.UtcNow.ToString("o")
});
}
}