Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
163 lines (140 loc) · 5.05 KB
/
Copy pathProgram.cs
File metadata and controls
163 lines (140 loc) · 5.05 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using CourseCore.Api.Modules.Access;
using CourseCore.Api.Modules.AuditLogs;
using CourseCore.Api.Modules.Auth;
using CourseCore.Api.Modules.Certificates;
using CourseCore.Api.Modules.Testimonials;
using CourseCore.Api.Modules.Courses;
using CourseCore.Api.Modules.Media;
using CourseCore.Api.Modules.Payments;
using CourseCore.Api.Modules.Progress;
using CourseCore.Api.Modules.Questions;
using CourseCore.Api.Modules.Users;
using CourseCore.Api.Modules.Visitors;
using CourseCore.Api.Shared;
using CourseCore.Api.Shared.Infrastructure.Configuration;
using CourseCore.Api.Shared.Infrastructure.Persistence.Seed;
using CourseCore.Api.Shared.Application.Validation;
using CourseCore.Api.Shared.Presentation.Health;
using CourseCore.Api.Shared.Presentation.Middleware;
using CourseCore.Api.Shared.Presentation.OpenApi;
using CourseCore.Api.Shared.Presentation.RateLimiting;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Scalar.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
const string CorsPolicyName = "CourseCoreCorsPolicy";
builder.WebHost.ConfigureKestrel(options =>
{
options.Limits.MaxRequestBodySize = ApplicationValidationLimits.MaxRequestBodySizeBytes;
});
if (builder.Environment.IsProduction())
{
builder.Configuration.ValidateProductionConfiguration();
}
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddCors(options =>
{
options.AddPolicy(CorsPolicyName, policy =>
{
var allowedOrigins = builder.Configuration
.GetSection("Cors:AllowedOrigins")
.Get<string[]>() ?? [];
if (builder.Environment.IsDevelopment() && allowedOrigins.Length == 0)
{
allowedOrigins =
[
"http://localhost:3000",
"https://localhost:3000"
];
}
policy
.WithOrigins(allowedOrigins)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
builder.Services
.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy(), tags: ["live"])
.AddCheck<CourseCoreDbContextHealthCheck>("database", tags: ["ready", "database"]);
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi(options =>
{
options.AddDocumentTransformer<BearerSecuritySchemeTransformer>();
options.AddOperationTransformer<BearerSecurityOperationTransformer>();
});
builder.Services.AddSharedInfrastructure(builder.Configuration);
builder.Services.AddCourseCoreRateLimiting(builder.Configuration);
// Only meaningful when ASPNETCORE_FORWARDEDHEADERS_ENABLED=true. The default (1) trusts just
// the last proxy hop. Behind two hops (Vercel rewrite -> Cloud Run) set it to 2 so the rate
// limiter sees the real client IP instead of the first proxy's.
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
var forwardLimit = builder.Configuration.GetValue<int?>("ForwardedHeaders:ForwardLimit");
if (forwardLimit is >= 1)
{
options.ForwardLimit = forwardLimit;
}
});
builder.Services.AddAuthModule(builder.Configuration);
builder.Services.AddUsersModule();
builder.Services.AddAccessModule();
builder.Services.AddCoursesModule();
builder.Services.AddMediaModule(builder.Configuration, builder.Environment);
builder.Services.AddProgressModule(builder.Configuration);
builder.Services.AddAuditLogsModule();
builder.Services.AddCertificatesModule();
builder.Services.AddTestimonialsModule();
builder.Services.AddQuestionsModule();
builder.Services.AddVisitorsModule();
builder.Services.AddPaymentsModule(builder.Configuration);
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapScalarApiReference(options =>
{
options.WithTitle("CourseCore API");
});
await app.SeedCourseCoreDatabaseAsync();
}
app.UseMiddleware<CorrelationIdMiddleware>();
app.UseMiddleware<ExceptionHandlingMiddleware>();
if (!app.Environment.IsDevelopment())
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors(CorsPolicyName);
app.UseAuthentication();
// After authentication so per-user limits can identify the caller.
app.UseRateLimiter();
app.UseAuthorization();
app.MapControllers();
app.MapHealthChecks("/health/live", new HealthCheckOptions
{
Predicate = check => check.Tags.Contains("live"),
ResponseWriter = HealthCheckResponseWriter.WriteMinimalAsync
});
app.MapHealthChecks("/health/ready", new HealthCheckOptions
{
Predicate = check => check.Tags.Contains("ready"),
ResponseWriter = app.Environment.IsDevelopment()
? HealthCheckResponseWriter.WriteDetailedAsync
: HealthCheckResponseWriter.WriteMinimalAsync
});
app.MapHealthChecks("/health", new HealthCheckOptions
{
ResponseWriter = app.Environment.IsDevelopment()
? HealthCheckResponseWriter.WriteDetailedAsync
: HealthCheckResponseWriter.WriteMinimalAsync
});
app.Run();
public partial class Program
{
}