| | | 1 | | using ClutterStock.Api.Extensions; |
| | | 2 | | using ClutterStock.Api.Generated; |
| | | 3 | | using ClutterStock.Infrastructure.Database; |
| | | 4 | | using ClutterStock.Infrastructure.Extensions; |
| | | 5 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | | 6 | | using Microsoft.AspNetCore.Authorization; |
| | | 7 | | using Microsoft.AspNetCore.Diagnostics.HealthChecks; |
| | | 8 | | using Microsoft.AspNetCore.Http.Features; |
| | | 9 | | using Microsoft.EntityFrameworkCore; |
| | | 10 | | |
| | 3 | 11 | | var builder = WebApplication.CreateBuilder(args); |
| | | 12 | | |
| | 3 | 13 | | builder.Services.AddOpenTelemetryObservability(builder.Environment); |
| | | 14 | | |
| | 3 | 15 | | var allowedOrigins = builder.Configuration.GetSection("Cors:AllowedOrigins").Get<string[]>() ?? []; |
| | 3 | 16 | | builder.Services.AddCors(options => |
| | 3 | 17 | | options.AddDefaultPolicy(policy => |
| | 3 | 18 | | { |
| | 3 | 19 | | if (allowedOrigins.Length > 0) |
| | 3 | 20 | | policy.WithOrigins(allowedOrigins).AllowAnyHeader().AllowAnyMethod(); |
| | 3 | 21 | | })); |
| | | 22 | | |
| | 3 | 23 | | builder.Services |
| | 3 | 24 | | .AddAuthentication(JwtBearerDefaults.AuthenticationScheme) |
| | 3 | 25 | | .AddJwtBearer(options => |
| | 3 | 26 | | { |
| | 3 | 27 | | options.Authority = builder.Configuration["Authentication:Authority"]; |
| | 3 | 28 | | options.Audience = builder.Configuration["Authentication:Audience"]; |
| | 3 | 29 | | }); |
| | | 30 | | |
| | 3 | 31 | | builder.Services.AddAuthorization(options => |
| | 3 | 32 | | options.FallbackPolicy = new AuthorizationPolicyBuilder() |
| | 3 | 33 | | .RequireAuthenticatedUser() |
| | 3 | 34 | | .Build()); |
| | | 35 | | |
| | 3 | 36 | | builder.Services.AddInfrastructure( |
| | 3 | 37 | | builder.Configuration.GetConnectionString("ClutterStock") ?? throw new InvalidOperationException("Missing connection |
| | | 38 | | |
| | 3 | 39 | | builder.Services.AddHealthChecks() |
| | 3 | 40 | | .AddDbContextCheck<ApplicationContext>(tags: ["ready"]); |
| | | 41 | | |
| | 3 | 42 | | builder.Services.AddDomainHandlers(); |
| | 3 | 43 | | builder.Services.AddControllers(); |
| | 3 | 44 | | builder.Services.AddProblemDetails(options => |
| | 3 | 45 | | options.CustomizeProblemDetails = ctx => |
| | 3 | 46 | | { |
| | 3 | 47 | | ctx.ProblemDetails.Instance ??= $"{ctx.HttpContext.Request.Method} {ctx.HttpContext.Request.Path}"; |
| | 3 | 48 | | ctx.ProblemDetails.Extensions.TryAdd("requestId", ctx.HttpContext.TraceIdentifier); |
| | 3 | 49 | | var activity = ctx.HttpContext.Features.Get<IHttpActivityFeature>()?.Activity; |
| | 3 | 50 | | if (activity is not null) |
| | 3 | 51 | | ctx.ProblemDetails.Extensions.TryAdd("traceId", activity.Id); |
| | 3 | 52 | | }); |
| | 3 | 53 | | builder.Services.AddValidation(); |
| | 3 | 54 | | builder.Services.AddOpenApiDocumentation(); |
| | | 55 | | |
| | 3 | 56 | | var app = builder.Build(); |
| | | 57 | | |
| | 3 | 58 | | app.UseExceptionHandler(); |
| | 3 | 59 | | app.UseStatusCodePages(); |
| | 3 | 60 | | app.UseCors(); |
| | 3 | 61 | | app.UseOpenApiDocumentation(); |
| | 3 | 62 | | app.UseAuthentication(); |
| | 3 | 63 | | app.UseAuthorization(); |
| | | 64 | | |
| | 3 | 65 | | if (app.Environment.IsDevelopment()) |
| | | 66 | | { |
| | 0 | 67 | | using var scope = app.Services.CreateScope(); |
| | 0 | 68 | | var db = scope.ServiceProvider.GetRequiredService<ApplicationContext>(); |
| | 0 | 69 | | await db.Database.MigrateAsync(); |
| | 0 | 70 | | } |
| | | 71 | | |
| | 3 | 72 | | app.UseHttpsRedirection(); |
| | | 73 | | |
| | 3 | 74 | | var liveness = new HealthCheckOptions |
| | 3 | 75 | | { |
| | 3 | 76 | | Predicate = static _ => false |
| | 3 | 77 | | }; |
| | | 78 | | |
| | 3 | 79 | | var readiness = new HealthCheckOptions |
| | 3 | 80 | | { |
| | 3 | 81 | | Predicate = static r => r.Tags.Contains("ready") |
| | 3 | 82 | | }; |
| | | 83 | | |
| | 3 | 84 | | app.MapHealthChecks("/healthz", liveness) |
| | 3 | 85 | | .WithTags("Health") |
| | 3 | 86 | | .AllowAnonymous(); |
| | | 87 | | |
| | 3 | 88 | | app.MapHealthChecks("/healthz/live", liveness) |
| | 3 | 89 | | .WithTags("Health") |
| | 3 | 90 | | .AllowAnonymous(); |
| | | 91 | | |
| | 3 | 92 | | app.MapHealthChecks("/healthz/ready", readiness) |
| | 3 | 93 | | .WithTags("Health") |
| | 3 | 94 | | .AllowAnonymous(); |
| | | 95 | | |
| | 3 | 96 | | app.MapDiscoveredEndpoints(); |
| | 3 | 97 | | app.MapControllers(); |
| | | 98 | | |
| | 3 | 99 | | app.Run(); |
| | | 100 | | |
| | | 101 | | namespace ClutterStock.Api |
| | | 102 | | { |
| | | 103 | | /// <summary>Exposed so test projects can target <c>WebApplicationFactory<Program></c>.</summary> |
| | | 104 | | [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] |
| | | 105 | | public partial class Program; |
| | | 106 | | } |