< Summary

Information
Class: Program
Assembly: Api
File(s): /home/runner/work/ClutterStock/ClutterStock/backend/src/Api/Program.cs
Tag: 58_25416222083
Line coverage
94%
Covered lines: 66
Uncovered lines: 4
Coverable lines: 70
Total lines: 106
Line coverage: 94.2%
Branch coverage
50%
Covered branches: 3
Total branches: 6
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
<Main>$()50%6694.28%

File(s)

/home/runner/work/ClutterStock/ClutterStock/backend/src/Api/Program.cs

#LineLine coverage
 1using ClutterStock.Api.Extensions;
 2using ClutterStock.Api.Generated;
 3using ClutterStock.Infrastructure.Database;
 4using ClutterStock.Infrastructure.Extensions;
 5using Microsoft.AspNetCore.Authentication.JwtBearer;
 6using Microsoft.AspNetCore.Authorization;
 7using Microsoft.AspNetCore.Diagnostics.HealthChecks;
 8using Microsoft.AspNetCore.Http.Features;
 9using Microsoft.EntityFrameworkCore;
 10
 311var builder = WebApplication.CreateBuilder(args);
 12
 313builder.Services.AddOpenTelemetryObservability(builder.Environment);
 14
 315var allowedOrigins = builder.Configuration.GetSection("Cors:AllowedOrigins").Get<string[]>() ?? [];
 316builder.Services.AddCors(options =>
 317    options.AddDefaultPolicy(policy =>
 318    {
 319        if (allowedOrigins.Length > 0)
 320            policy.WithOrigins(allowedOrigins).AllowAnyHeader().AllowAnyMethod();
 321    }));
 22
 323builder.Services
 324    .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
 325    .AddJwtBearer(options =>
 326    {
 327        options.Authority = builder.Configuration["Authentication:Authority"];
 328        options.Audience = builder.Configuration["Authentication:Audience"];
 329    });
 30
 331builder.Services.AddAuthorization(options =>
 332    options.FallbackPolicy = new AuthorizationPolicyBuilder()
 333        .RequireAuthenticatedUser()
 334        .Build());
 35
 336builder.Services.AddInfrastructure(
 337    builder.Configuration.GetConnectionString("ClutterStock") ?? throw new InvalidOperationException("Missing connection
 38
 339builder.Services.AddHealthChecks()
 340       .AddDbContextCheck<ApplicationContext>(tags: ["ready"]);
 41
 342builder.Services.AddDomainHandlers();
 343builder.Services.AddControllers();
 344builder.Services.AddProblemDetails(options =>
 345    options.CustomizeProblemDetails = ctx =>
 346    {
 347        ctx.ProblemDetails.Instance ??= $"{ctx.HttpContext.Request.Method} {ctx.HttpContext.Request.Path}";
 348        ctx.ProblemDetails.Extensions.TryAdd("requestId", ctx.HttpContext.TraceIdentifier);
 349        var activity = ctx.HttpContext.Features.Get<IHttpActivityFeature>()?.Activity;
 350        if (activity is not null)
 351            ctx.ProblemDetails.Extensions.TryAdd("traceId", activity.Id);
 352    });
 353builder.Services.AddValidation();
 354builder.Services.AddOpenApiDocumentation();
 55
 356var app = builder.Build();
 57
 358app.UseExceptionHandler();
 359app.UseStatusCodePages();
 360app.UseCors();
 361app.UseOpenApiDocumentation();
 362app.UseAuthentication();
 363app.UseAuthorization();
 64
 365if (app.Environment.IsDevelopment())
 66{
 067    using var scope = app.Services.CreateScope();
 068    var db = scope.ServiceProvider.GetRequiredService<ApplicationContext>();
 069    await db.Database.MigrateAsync();
 070}
 71
 372app.UseHttpsRedirection();
 73
 374var liveness = new HealthCheckOptions
 375{
 376    Predicate = static _ => false
 377};
 78
 379var readiness = new HealthCheckOptions
 380{
 381    Predicate = static r => r.Tags.Contains("ready")
 382};
 83
 384app.MapHealthChecks("/healthz", liveness)
 385   .WithTags("Health")
 386   .AllowAnonymous();
 87
 388app.MapHealthChecks("/healthz/live", liveness)
 389   .WithTags("Health")
 390   .AllowAnonymous();
 91
 392app.MapHealthChecks("/healthz/ready", readiness)
 393   .WithTags("Health")
 394   .AllowAnonymous();
 95
 396app.MapDiscoveredEndpoints();
 397app.MapControllers();
 98
 399app.Run();
 100
 101namespace ClutterStock.Api
 102{
 103    /// <summary>Exposed so test projects can target <c>WebApplicationFactory&lt;Program&gt;</c>.</summary>
 104    [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
 105    public partial class Program;
 106}

Methods/Properties

<Main>$()