2026-07-18 14:11:47 +00:00
|
|
|
using Heimdall.Agent;
|
2026-07-16 20:53:56 +00:00
|
|
|
using Heimdall.Agent.Services;
|
2026-07-18 14:09:32 +00:00
|
|
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
|
|
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
2026-07-18 14:11:47 +00:00
|
|
|
using Microsoft.Extensions.Options;
|
2026-07-16 19:09:28 +00:00
|
|
|
|
2026-07-18 14:09:32 +00:00
|
|
|
var builder = WebApplication.CreateSlimBuilder(args);
|
2026-07-16 20:53:56 +00:00
|
|
|
|
2026-07-18 14:11:47 +00:00
|
|
|
// Strongly-typed configuration with validation
|
|
|
|
|
builder.Services.AddOptions<AgentOptions>()
|
|
|
|
|
.BindConfiguration(AgentOptions.SectionName)
|
|
|
|
|
.ValidateDataAnnotations()
|
|
|
|
|
.ValidateOnStart();
|
|
|
|
|
|
2026-07-18 14:09:32 +00:00
|
|
|
// HTTP clients
|
2026-07-18 14:11:47 +00:00
|
|
|
builder.Services.AddHttpClient("Central", (sp, client) =>
|
2026-07-16 20:53:56 +00:00
|
|
|
{
|
2026-07-18 14:11:47 +00:00
|
|
|
var options = sp.GetRequiredService<IOptions<AgentOptions>>().Value;
|
|
|
|
|
client.BaseAddress = new Uri(options.CentralUrl.TrimEnd('/') + "/");
|
2026-07-16 20:53:56 +00:00
|
|
|
client.Timeout = TimeSpan.FromSeconds(10);
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-18 14:02:40 +00:00
|
|
|
builder.Services.AddHttpClient("IpResolver", client =>
|
|
|
|
|
{
|
|
|
|
|
client.Timeout = TimeSpan.FromSeconds(5);
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-18 14:09:32 +00:00
|
|
|
// Agent services
|
2026-07-18 14:11:47 +00:00
|
|
|
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>();
|
2026-07-16 20:53:56 +00:00
|
|
|
builder.Services.AddHostedService<AgentRegistrationService>();
|
2026-07-18 18:58:20 +00:00
|
|
|
builder.Services.AddHostedService<ConfigReceiverService>();
|
2026-07-16 19:09:28 +00:00
|
|
|
|
2026-07-18 14:09:32 +00:00
|
|
|
// Health checks — used by Docker HEALTHCHECK
|
|
|
|
|
builder.Services.AddHealthChecks()
|
|
|
|
|
.AddCheck<AgentHealthCheck>("agent_health", tags: ["ready"]);
|
|
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
2026-07-18 14:11:47 +00:00
|
|
|
// Health endpoint
|
2026-07-18 14:09:32 +00:00
|
|
|
app.MapHealthChecks("/healthz", new HealthCheckOptions
|
|
|
|
|
{
|
|
|
|
|
Predicate = _ => true,
|
|
|
|
|
ResultStatusCodes =
|
|
|
|
|
{
|
|
|
|
|
[HealthStatus.Healthy] = StatusCodes.Status200OK,
|
|
|
|
|
[HealthStatus.Degraded] = StatusCodes.Status200OK,
|
|
|
|
|
[HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable,
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await app.RunAsync();
|