using Sentry.AspNetCore; using Heimdall.Agent; using Heimdall.Agent.Logging; using Heimdall.Agent.Services; using Heimdall.Application.Discovery; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; var builder = WebApplication.CreateSlimBuilder(args); // ── Sentry (GlitchTip) — DSN from config pipeline with legacy env var fallback ── // Primary: Sentry:Dsn from appsettings.json or env var Sentry__Dsn // Fallback: legacy SENTRY_DSN env var (Docker backward compat) // Neither set → DSN is null → Sentry SDK no-ops gracefully var sentryDsn = builder.Configuration["Sentry:Dsn"] ?? Environment.GetEnvironmentVariable("SENTRY_DSN"); if (!string.IsNullOrEmpty(sentryDsn)) { builder.WebHost.UseSentry((SentryAspNetCoreOptions options) => { options.Dsn = sentryDsn; options.MinimumBreadcrumbLevel = LogLevel.Debug; options.MinimumEventLevel = LogLevel.Error; options.TracesSampleRate = 0.05; options.MaxRequestBodySize = Sentry.Extensibility.RequestSize.Small; options.ProfilesSampleRate = 0.05; }); } // 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.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(sp => sp.GetRequiredService()); builder.Services.AddSingleton(); builder.Services.AddSingleton(sp => sp.GetRequiredService()); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddHostedService(); builder.Services.AddHostedService(); builder.Services.AddHostedService(); // Video capture pipeline (ticket #1): FFmpeg capture per assigned camera, // bounded snapshot channel, adaptive interval, frames → FrameAnalyzerService. builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddHostedService(); // Log capture & upload to Central (ticket #50) builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(sp => sp.GetRequiredService()); 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();