Refactor CORS config; add architectural commentary
Refactored CORS setup to be environment-aware, restricting origins in production and relaxing in development. Added extensive comments and discussion on service and repository layer design, including clean architecture best practices and CQRS/MediatR considerations. No changes to business logic; documentation and intent clarified for maintainers.
This commit is contained in:
@@ -14,20 +14,22 @@ builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
// TODO: allow listed origins configured in appsettings.json
|
||||
// In any case, dont let them to free to use without cors. if there is no origin specified, block all.
|
||||
// In development you can keep it easy.
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
var origins = builder.Configuration.GetSection("Cors:AllowedOrigins").Get<string[]>() ?? Array.Empty<string>();
|
||||
{
|
||||
options.AddDefaultPolicy(policy =>
|
||||
{
|
||||
if (origins.Length > 0)
|
||||
if(builder.Environment.IsDevelopment())
|
||||
{
|
||||
policy.WithOrigins(origins)
|
||||
policy.AllowAnyOrigin()
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod();
|
||||
}
|
||||
else
|
||||
{
|
||||
policy.AllowAnyOrigin()
|
||||
var origins = builder.Configuration.GetSection("Cors:AllowedOrigins").Get<string[]>() ?? [];
|
||||
policy.WithOrigins(origins)
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user