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

76 lines
2.5 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;
// ── Sentry (GlitchTip) — DSN from env var, with hardcoded fallback ─
var sentryDsn = Environment.GetEnvironmentVariable("SENTRY_DSN")
?? "https://db94b9fc2f36425f9eb4b9f6f347dc73@glitchtip.tordais.cc/6";
var builder = WebApplication.CreateSlimBuilder(args);
// ── Sentry ────────────────────────────────────────────────────────
builder.WebHost.UseSentry(options =>
{
options.Dsn = sentryDsn;
options.MinimumBreadcrumbLevel = LogLevel.Debug;
options.MinimumEventLevel = LogLevel.Error;
options.TracesSampleRate = 0.1;
});
// 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>();
builder.Services.AddHostedService<ConfigReceiverService>();
// 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();