Skip to content

Commit

Permalink
feat: add CorsMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
danielNg91 committed Dec 16, 2023
1 parent 4843657 commit 28f9364
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Api/Middlewares/CorsMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class CorsMiddleware
{
private readonly RequestDelegate _next;

public CorsMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task Invoke(HttpContext context)
{
context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
context.Response.Headers.Add("Access-Control-Allow-Methods", "*");
context.Response.Headers.Add("Access-Control-Allow-Headers", "*");

// Handle pre-flight requests
if (context.Request.Method == "OPTIONS")
{
context.Response.StatusCode = 200;
await context.Response.CompleteAsync();
return;
}

// Call the next middleware in the pipeline
await _next(context);
}
}
1 change: 1 addition & 0 deletions Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
await app.Services.DbInitializer();
}

app.UseMiddleware<CorsMiddleware>();
app.UseLoggingInterceptor();
app.UseCors(Constants.Http.CORS);
app.UseAutoWrapper();
Expand Down

0 comments on commit 28f9364

Please sign in to comment.