Skip to content

Commit 7445c27

Browse files
chore: update cors
1 parent d5da89c commit 7445c27

6 files changed

Lines changed: 51 additions & 15 deletions

File tree

src/Api/Extensions/ProblemDetailExtention.cs renamed to src/Api/Extensions/ProblemDetailExtension.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
using System.Diagnostics;
21
using Api.Middlewares;
32
using Microsoft.AspNetCore.Http.Features;
43

54
namespace Api.Extensions;
65

7-
public static class ProblemDetailExtention
6+
public static class ProblemDetailExtension
87
{
98
public static IServiceCollection AddErrorDetails(this IServiceCollection services)
109
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Api.Extensions;
55

6-
public static class SerialogExtension
6+
public static class SerilogExtension
77
{
88
public static void AddSerilog(this WebApplicationBuilder builder)
99
{

src/Api/Program.cs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Api.common.Routers;
44
using Api.Converters;
55
using Api.Extensions;
6+
using Api.Settings;
67
using Application;
78
using Cysharp.Serialization.Json;
89
using HealthChecks.UI.Client;
@@ -37,19 +38,33 @@
3738
builder.AddSerilog();
3839
services.AddHealthChecks();
3940
services.AddDatabaseHealthCheck(configuration);
41+
42+
List<CorsProfileSettings> corsProfiles =
43+
configuration.GetSection(nameof(CorsProfileSettings)).Get<List<CorsProfileSettings>>()
44+
??
45+
[
46+
new CorsProfileSettings()
47+
{
48+
Name = "AllowClientWith3000Port",
49+
Origin = "http://localhost:3000",
50+
},
51+
];
4052
services.AddCors(options =>
4153
{
42-
options.AddPolicy(
43-
"AllowLocalhost3000",
44-
policy =>
45-
{
46-
policy
47-
.WithOrigins("http://localhost:3000")
48-
.AllowAnyMethod()
49-
.AllowAnyHeader()
50-
.AllowCredentials();
51-
}
52-
);
54+
foreach (CorsProfileSettings profile in corsProfiles)
55+
{
56+
options.AddPolicy(
57+
profile.Name!,
58+
policy =>
59+
{
60+
policy
61+
.WithOrigins(profile.Origin!)
62+
.AllowAnyMethod()
63+
.AllowAnyHeader()
64+
.AllowCredentials();
65+
}
66+
);
67+
}
5368
});
5469
#endregion
5570

@@ -98,7 +113,10 @@
98113
app.AddLog(Log.Logger, routeRefix, healthCheckPath);
99114
}
100115

101-
app.UseCors("AllowLocalhost3000");
116+
foreach (var profile in corsProfiles)
117+
{
118+
app.UseCors(profile.Name!);
119+
}
102120
app.UseStatusCodePages();
103121
app.UseExceptionHandler();
104122
app.UseStaticFiles();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Api.Settings;
2+
3+
public class CorsProfileSettings
4+
{
5+
public string? Name { get; set; }
6+
public string? Origin { get; set; }
7+
}

src/Api/appsettings.Testing-Deployment.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{
22
"urls": "http://0.0.0.0:8080",
3+
"CorsProfileSettings": [
4+
{
5+
"Name": "AllowClientWith3000Port",
6+
"Origin": "http://localhost:3000"
7+
}
8+
],
39
"Serilog": {
410
"Using": ["Serilog.Sinks.Console"],
511
"MinimumLevel": {

src/Api/appsettings.example.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{
22
"urls": "http://0.0.0.0:8080",
3+
"CorsProfileSettings": [
4+
{
5+
"Name": "AllowClientWith3000Port",
6+
"Origin": "http://localhost:3000"
7+
}
8+
],
39
"Serilog": {
410
"Using": ["Serilog.Sinks.Console"],
511
"MinimumLevel": {

0 commit comments

Comments
 (0)