Replace new Uri(url.TrimEnd('/') + "/") pattern with explicit
EndsWith('/') check to avoid string concatenation inside URI
construction. Applies to 3 locations:
- Program.cs (Heimdall.Web) - HttpClient base address
- TokenRefreshHandler.cs - raw HttpClient for token refresh
- Program.cs (Heimdall.Agent) - Central service client
80 lines
2.6 KiB
C#
80 lines
2.6 KiB
C#
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<AgentOptions>()
|
|
.BindConfiguration(AgentOptions.SectionName)
|
|
.ValidateDataAnnotations()
|
|
.ValidateOnStart();
|
|
|
|
// HTTP clients
|
|
builder.Services.AddHttpClient("Central", (sp, client) =>
|
|
{
|
|
var options = sp.GetRequiredService<IOptions<AgentOptions>>().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<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();
|