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 only ─────────────────── var sentryDsn = Environment.GetEnvironmentVariable("SENTRY_DSN"); var builder = WebApplication.CreateSlimBuilder(args); // ── Sentry ──────────────────────────────────────────────────────── if (sentryDsn is not null) { 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() .BindConfiguration(AgentOptions.SectionName) .ValidateDataAnnotations() .ValidateOnStart(); // HTTP clients builder.Services.AddHttpClient("Central", (sp, client) => { var options = sp.GetRequiredService>().Value; var baseUrl = options.CentralUrl.EndsWith('/') ? options.CentralUrl : options.CentralUrl + "/"; client.BaseAddress = new Uri(baseUrl); client.Timeout = TimeSpan.FromSeconds(10); }); builder.Services.AddHttpClient("IpResolver", client => { client.Timeout = TimeSpan.FromSeconds(5); }); // Agent services builder.Services.AddSingleton(sp => { var state = new AgentState(); var options = sp.GetRequiredService>().Value; var stateDir = options.StateDirectory ?? (OperatingSystem.IsLinux() ? "/var/lib/heimdall" : Directory.GetCurrentDirectory()); state.ConfigureStateDirectory(stateDir); return state; }); builder.Services.AddSingleton(); builder.Services.AddHostedService(); builder.Services.AddHostedService(); // Health checks — used by Docker HEALTHCHECK builder.Services.AddHealthChecks() .AddCheck("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();