Files
Heimdall/src/Heimdall.Agent/Program.cs
T

62 lines
1.8 KiB
C#
Raw Normal View History

using Heimdall.Agent;
using Heimdall.Agent.Services;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Options;
var builder = WebApplication.CreateSlimBuilder(args);
// Strongly-typed configuration with validation
builder.Services.AddOptions<AgentOptions>()
.BindConfiguration(AgentOptions.SectionName)
.ValidateDataAnnotations()
.ValidateOnStart();
// HTTP clients
builder.Services.AddHttpClient("Central", (sp, client) =>
{
var options = sp.GetRequiredService<IOptions<AgentOptions>>().Value;
client.BaseAddress = new Uri(options.CentralUrl.TrimEnd('/') + "/");
client.Timeout = TimeSpan.FromSeconds(10);
});
builder.Services.AddHttpClient("IpResolver", client =>
{
client.Timeout = TimeSpan.FromSeconds(5);
});
// Agent services
builder.Services.AddSingleton<AgentState>(sp =>
{
var state = new AgentState();
var options = sp.GetRequiredService<IOptions<AgentOptions>>().Value;
var stateDir = options.StateDirectory
?? (OperatingSystem.IsLinux() ? "/var/lib/heimdall" : Directory.GetCurrentDirectory());
state.ConfigureStateDirectory(stateDir);
return state;
});
builder.Services.AddSingleton<SystemMetricsProvider>();
builder.Services.AddHostedService<AgentRegistrationService>();
// Health checks — used by Docker HEALTHCHECK
builder.Services.AddHealthChecks()
.AddCheck<AgentHealthCheck>("agent_health", tags: ["ready"]);
var app = builder.Build();
// Health endpoint
app.MapHealthChecks("/healthz", new HealthCheckOptions
{
Predicate = _ => true,
ResultStatusCodes =
{
[HealthStatus.Healthy] = StatusCodes.Status200OK,
[HealthStatus.Degraded] = StatusCodes.Status200OK,
[HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable,
}
});
await app.RunAsync();