-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
145 lines (126 loc) · 4.81 KB
/
Program.cs
File metadata and controls
145 lines (126 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using CCE.Api.Common.Auth;
using CCE.Api.Common.Authorization;
using CCE.Api.Common.Health;
using CCE.Api.Common.Identity;
using CCE.Api.Common.Middleware;
using CCE.Api.Common.Observability;
using CCE.Api.Common.OpenApi;
using CCE.Api.Common.RateLimiting;
using CCE.Api.Internal.Endpoints;
using CCE.Application;
using CCE.Application.Common.CountryScope;
using CCE.Application.Common.Interfaces;
using CCE.Application.Health;
using CCE.Infrastructure;
using CCE.Infrastructure.Search;
using MediatR;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Serilog;
using System.Globalization;
using System.Text.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseCceSerilog();
builder.Services.ConfigureHttpJsonOptions(opts =>
{
opts.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
builder.Services
.AddApplication()
.AddInfrastructure(builder.Configuration)
.AddCceMeilisearchIndexer()
.AddCceJwtAuth(builder.Configuration, CCE.Application.Identity.Auth.Common.LocalAuthApi.Internal)
.AddCcePermissionPolicies()
.AddCceUserSync()
.AddCceHealthChecks(builder.Configuration)
.AddCceOpenTelemetry(builder.Configuration, "CCE.Api.Internal")
.AddCceRateLimiter(builder.Configuration)
.AddCceOpenApi("CCE Internal API");
builder.Services.AddHttpContextAccessor();
builder.Services.Replace(ServiceDescriptor.Scoped<ICurrentUserAccessor, HttpContextCurrentUserAccessor>());
builder.Services.Replace(ServiceDescriptor.Scoped<ICountryScopeAccessor, HttpContextCountryScopeAccessor>());
builder.Services.AddSignalR().AddJsonProtocol();
var app = builder.Build();
// Middleware order (spec §7.1): correlation → exception → security headers → auth → authz → rate → locale
app.UseMiddleware<CorrelationIdMiddleware>();
app.UseSerilogRequestLogging();
app.UseMiddleware<ExceptionHandlingMiddleware>();
app.UseMiddleware<SecurityHeadersMiddleware>();
app.UseAuthentication();
app.UseAuthorization();
app.UseCceUserSync();
app.UseRateLimiter();
app.UseCcePrometheus();
app.UseMiddleware<LocalizationMiddleware>();
app.UseStaticFiles();
app.UseCceOpenApi(apiTag: "internal");
app.MapAuthEndpoints(CCE.Application.Identity.Auth.Common.LocalAuthApi.Internal);
app.MapAdminAuthEndpoints();
app.MapIdentityEndpoints();
app.MapExpertEndpoints();
app.MapAssetEndpoints();
app.MapResourceEndpoints();
app.MapResourceCategoryEndpoints();
app.MapCountryResourceRequestEndpoints();
app.MapCountryEndpoints();
app.MapCountryProfileEndpoints();
app.MapNewsEndpoints();
app.MapEventEndpoints();
app.MapPageEndpoints();
app.MapHomepageSectionEndpoints();
app.MapTopicEndpoints();
app.MapCommunityModerationEndpoints();
app.MapNotificationTemplateEndpoints();
app.MapNotificationLogEndpoints();
app.MapReportEndpoints();
app.MapAuditEndpoints();
app.MapHomepageSettingsEndpoints();
app.MapAboutSettingsEndpoints();
app.MapPoliciesSettingsEndpoints();
app.MapMediaEndpoints();
app.MapCountryCodeEndpoints();
app.MapEvaluationEndpoints();
// Sub-11d follow-up — dev sign-in shim. Mounts /dev/sign-in,
// /dev/sign-out, /dev/whoami when Auth:DevMode=true. Production
// deployments leave the flag false → endpoints are never mounted.
if (builder.Configuration.GetValue<bool>("Auth:DevMode"))
{
app.MapDevAuthEndpoints();
}
app.MapGet("/", () => "CCE.Api.Internal — Foundation");
app.MapGet("/auth/echo", (HttpContext ctx) =>
{
var name = ctx.User.Identity?.Name ?? "(no name)";
var upn = ctx.User.FindFirst("upn")?.Value ?? "(no upn)";
return Results.Ok(new { name, upn });
}).RequireAuthorization();
app.MapGet("/health", async (IMediator mediator) =>
{
var locale = CultureInfo.CurrentCulture.Name;
var result = await mediator.Send(new HealthQuery(Locale: locale)).ConfigureAwait(false);
return Results.Ok(result);
});
app.MapHealthChecks("/health/ready", new Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckOptions
{
Predicate = check => check.Tags.Contains("ready")
});
app.MapGet("/health/authenticated", async (IMediator mediator, HttpContext ctx) =>
{
var user = ctx.User;
var groups = user.FindAll("groups").Select(c => c.Value).ToList();
var locale = CultureInfo.CurrentCulture.Name;
var query = new AuthenticatedHealthQuery(
UserId: user.FindFirst("sub")?.Value ?? "(no sub)",
PreferredUsername: user.FindFirst("preferred_username")?.Value ?? "(no name)",
Email: user.FindFirst("email")?.Value ?? "(no email)",
Upn: user.FindFirst("upn")?.Value ?? "(no upn)",
Groups: groups,
Locale: locale);
var result = await mediator.Send(query).ConfigureAwait(false);
return Results.Ok(result);
})
.RequireAuthorization(policy => policy.RequireClaim("groups", "SuperAdmin"));
app.Run();
namespace CCE.Api.Internal
{
public partial class Program;
}