Customizable gateway endpoints

This commit is contained in:
2026-01-25 01:27:17 +08:00
parent b851b9f6e2
commit db3a6ea9c5
6 changed files with 160 additions and 7 deletions

View File

@@ -2,7 +2,8 @@ namespace DysonNetwork.Gateway.Health;
public abstract class GatewayConstant
{
public static readonly string[] ServiceNames =
// Default service names used when no configuration is provided
private static readonly string[] DefaultServiceNames =
[
"ring",
"pass",
@@ -14,12 +15,39 @@ public abstract class GatewayConstant
"messager"
];
// Core services stands with w/o these services the functional of entire app will broke.
public static readonly string[] CoreServiceNames =
// Default core service names used when no configuration is provided
private static readonly string[] DefaultCoreServiceNames =
[
"ring",
"pass",
"drive",
"sphere"
];
}
// Configuration-driven service names with fallback to defaults
public static string[] ServiceNames { get; private set; } = DefaultServiceNames;
// Configuration-driven core service names with fallback to defaults
public static string[] CoreServiceNames { get; private set; } = DefaultCoreServiceNames;
/// <summary>
/// Initializes the service names from configuration options.
/// This method should be called during application startup.
/// </summary>
/// <param name="options">The gateway endpoints options containing configuration</param>
public static void InitializeFromConfiguration(DysonNetwork.Gateway.Configuration.GatewayEndpointsOptions options)
{
ServiceNames = options.GetServiceNames();
CoreServiceNames = options.GetCoreServiceNames();
}
/// <summary>
/// Resets the service names to their default values.
/// Useful for testing or when configuration is not available.
/// </summary>
public static void ResetToDefaults()
{
ServiceNames = DefaultServiceNames;
CoreServiceNames = DefaultCoreServiceNames;
}
}

View File

@@ -57,4 +57,4 @@ public class GatewayHealthAggregator(IHttpClientFactory httpClientFactory, Gatew
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
}
}
}
}

View File

@@ -34,6 +34,42 @@ public class GatewayReadinessStore
InitializeServices(GatewayConstant.ServiceNames);
}
/// <summary>
/// Reinitializes the store with new service names from configuration.
/// This method should be called when configuration changes.
/// </summary>
/// <param name="serviceNames">The new service names to track</param>
public void ReinitializeServices(string[] serviceNames)
{
lock (_lock)
{
// Preserve existing health states for services that still exist
var existingStates = new Dictionary<string, ServiceHealthState>(_services);
_services.Clear();
foreach (var name in serviceNames)
{
// Use existing state if available, otherwise create new unhealthy state
if (existingStates.TryGetValue(name, out var existingState))
{
_services[name] = existingState;
}
else
{
_services[name] = new ServiceHealthState(
name,
IsHealthy: false,
LastChecked: SystemClock.Instance.GetCurrentInstant(),
Error: "Not checked yet"
);
}
}
RecalculateLocked();
}
}
private void InitializeServices(IEnumerable<string> serviceNames)
{
lock (_lock)