|
5 | 5 |
|
6 | 6 | /* ----------------------------------------------------------------------------- |
7 | 7 | * Web Application |
| 8 | + * Registers all services into the DI container before the application is built. |
| 9 | + * Throughout this section, builder.Services refers to the ASP.NET Core |
| 10 | + * dependency injection (DI) container — not to be confused with the Services |
| 11 | + * subsection below, which registers our own application-level business logic. |
8 | 12 | * https://learn.microsoft.com/en-us/aspnet/core/fundamentals/startup |
9 | 13 | * -------------------------------------------------------------------------- */ |
10 | 14 |
|
|
22 | 26 | Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).CreateLogger(); |
23 | 27 | builder.Host.UseSerilog(); |
24 | 28 |
|
25 | | -/* Controllers -------------------------------------------------------------- */ |
| 29 | +/* Infrastructure ----------------------------------------------------------- */ |
26 | 30 |
|
27 | 31 | builder.Services.AddHealthChecks(); |
28 | | -builder.Services.AddControllers(); |
29 | | -builder.Services.AddValidators(); |
30 | 32 | builder.Services.AddCorsDefaultPolicy(builder.Environment); |
31 | 33 | builder.Services.AddFixedWindowRateLimiter(builder.Configuration); |
32 | 34 |
|
|
35 | 37 | builder.Services.AddSwaggerConfiguration(builder.Configuration); |
36 | 38 | } |
37 | 39 |
|
38 | | -/* Services ----------------------------------------------------------------- */ |
| 40 | +/* Controllers -------------------------------------------------------------- */ |
| 41 | + |
| 42 | +builder.Services.AddControllers(); |
| 43 | +builder.Services.AddValidators(); |
| 44 | + |
| 45 | +/* Services (Business Logic) ------------------------------------------------ */ |
39 | 46 |
|
40 | 47 | builder.Services.RegisterPlayerService(); |
41 | 48 | builder.Services.AddMemoryCache(); |
|
53 | 60 |
|
54 | 61 | /* ----------------------------------------------------------------------------- |
55 | 62 | * Database Migration |
| 63 | + * Applies pending EF Core migrations at startup, before the app accepts requests. |
56 | 64 | * https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/applying#apply-migrations-at-runtime |
57 | 65 | * -------------------------------------------------------------------------- */ |
58 | 66 |
|
|
64 | 72 |
|
65 | 73 | /* ----------------------------------------------------------------------------- |
66 | 74 | * Middlewares |
| 75 | + * Defines the order in which middleware components process each HTTP request. |
67 | 76 | * https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware |
| 77 | + * https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware#middleware-order |
68 | 78 | * -------------------------------------------------------------------------- */ |
69 | 79 |
|
| 80 | +// Replaces the default ASP.NET Core request logging with Serilog's structured |
| 81 | +// logging, emitting one log entry per request with timing, status code, and |
| 82 | +// other contextual properties. |
70 | 83 | app.UseSerilogRequestLogging(); |
| 84 | + |
| 85 | +// Custom middleware that catches unhandled exceptions and returns a consistent |
| 86 | +// RFC 7807 Problem Details response instead of exposing a raw stack trace. |
71 | 87 | app.UseExceptionHandling(); |
| 88 | + |
| 89 | +// Redirects all plain HTTP requests to HTTPS, enforcing transport security. |
72 | 90 | app.UseHttpsRedirection(); |
| 91 | + |
| 92 | +// Intentionally registered before UseRateLimiter so that health check probes |
| 93 | +// always succeed regardless of rate-limiting thresholds, allowing monitoring |
| 94 | +// and orchestration systems to assess liveness and readiness without being |
| 95 | +// throttled. |
73 | 96 | app.MapHealthChecks("/health"); |
| 97 | + |
| 98 | +// Enforces the fixed-window rate limiting policy defined during service |
| 99 | +// registration, returning 429 Too Many Requests when the limit is exceeded. |
74 | 100 | app.UseRateLimiter(); |
75 | | -app.MapControllers(); |
76 | 101 |
|
77 | 102 | if (app.Environment.IsDevelopment()) |
78 | 103 | { |
| 104 | + // Only active in Development, where AddCorsDefaultPolicy registers a |
| 105 | + // permissive wildcard policy for Swagger UI and local frontends. No policy |
| 106 | + // exists in Production — the API is assumed to be consumed server-to-server |
| 107 | + // or behind a reverse proxy on the same origin, where CORS is not needed. |
| 108 | + // Must precede MapControllers so CORS headers are applied before any |
| 109 | + // endpoint executes, consistent with the standard middleware pipeline order. |
79 | 110 | app.UseCors(); |
| 111 | + |
| 112 | + // Generates the OpenAPI JSON document consumed by Swagger UI. |
80 | 113 | app.UseSwagger(); |
| 114 | + |
| 115 | + // Serves the interactive Swagger UI at /swagger, allowing manual |
| 116 | + // exploration and testing of the API endpoints during development. |
81 | 117 | app.UseSwaggerUI(); |
82 | 118 | } |
83 | 119 |
|
| 120 | +// Routes incoming HTTP requests to the matching controller actions. Must come |
| 121 | +// after all middleware that needs to run before endpoint execution (CORS, rate |
| 122 | +// limiting, etc.). |
| 123 | +app.MapControllers(); |
| 124 | + |
84 | 125 | await app.RunAsync(); |
0 commit comments