-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
99 lines (81 loc) · 3.44 KB
/
Program.cs
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
using ClubService.API;
using ClubService.API.ApplicationConfigurations;
using ClubService.Domain.Repository;
using ClubService.Infrastructure.DbContexts;
using Microsoft.AspNetCore.Authentication;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
if (builder.Environment.IsProduction())
{
builder.Configuration.AddJsonFile("appsettings.Production.json");
builder.Configuration.AddEnvironmentVariables();
builder.Configuration.AddSubstitution();
}
builder.Services.AddDatabaseConfigurations(builder.Configuration);
builder.Services.AddLoggingConfigurations(builder.Configuration);
builder.Services.AddRepositoryConfigurations();
builder.Services.AddServiceConfigurations();
builder.Services.AddTransactionConfigurations();
builder.Services.AddEventHandlerConfigurations();
builder.Services.AddExternalServiceConfigurations(builder.Configuration);
builder.Services.AddApiVersioningConfigurations();
builder.Services.AddSwaggerConfigurations();
builder.Services.AddExceptionHandlerConfigurations();
builder.Services.AddControllers();
// By configuring a default authentication scheme, we ensure that the authorization middleware works correctly
// and returns the appropriate 403 Forbidden response when the user does not have the required role.
// The custom middleware JwtClaimsMiddleware.cs continues to handle the extraction of claims from the JWT, while the dummy
// authentication scheme satisfies the requirement for a default authentication handler.
builder.Services
.AddAuthentication("BasicScheme")
.AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicScheme", null);
builder.Services.AddAuthorization();
var app = builder.Build();
using var scope = app.Services.CreateScope();
var services = scope.ServiceProvider;
var eventStoreDbContext = services.GetRequiredService<EventStoreDbContext>();
var readStoreDbContext = services.GetRequiredService<ReadStoreDbContext>();
var loginStoreDbContext = services.GetRequiredService<LoginStoreDbContext>();
if (builder.Environment.IsProduction())
{
if (eventStoreDbContext.Database.GetPendingMigrations().Any())
{
await eventStoreDbContext.Database.MigrateAsync();
}
if (readStoreDbContext.Database.GetPendingMigrations().Any())
{
await readStoreDbContext.Database.MigrateAsync();
}
if (loginStoreDbContext.Database.GetPendingMigrations().Any())
{
await loginStoreDbContext.Database.MigrateAsync();
}
}
else if (app.Environment.IsDevelopment() || app.Environment.IsEnvironment("DockerDevelopment"))
{
app.UseSwagger();
app.UseSwaggerUI(options => { options.SwaggerEndpoint("/swagger/v1/swagger.json", "ClubServiceV1"); });
await eventStoreDbContext.Database.EnsureCreatedAsync();
try
{
await eventStoreDbContext.SeedTestData();
}
catch (DbUpdateException)
{
var logger = services.GetRequiredService<ILoggerService<Program>>();
logger.LogDuplicateSeedData();
}
await readStoreDbContext.Database.EnsureDeletedAsync();
await readStoreDbContext.Database.EnsureCreatedAsync();
await loginStoreDbContext.Database.EnsureDeletedAsync();
await loginStoreDbContext.Database.EnsureCreatedAsync();
}
app.UseExceptionHandler();
app.UseMiddleware<JwtClaimsMiddleware>(); // Use custom middleware to extract JWT claims
app.UseAuthorization();
app.MapControllers();
await app.RunAsync();
// For integration tests
public abstract partial class Program
{
}