-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
68 lines (52 loc) · 1.61 KB
/
Program.cs
File metadata and controls
68 lines (52 loc) · 1.61 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
using Microsoft.AspNetCore.Diagnostics;
using TaskManagementAPI.Repositories;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<ITaskRepository, TaskRepository>();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowFrontend", policy =>
{
policy.WithOrigins(
"http://localhost:3000",
"https://task-management-frontend-swart-five.vercel.app")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
var app = builder.Build();
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
context.Response.StatusCode = 500;
context.Response.ContentType = "application/json";
var exceptionDetails = context.Features.Get<IExceptionHandlerFeature>()?.Error;
var response = new
{
message = "An unexpected error occurred.",
details = exceptionDetails?.Message
};
await context.Response.WriteAsJsonAsync(response).ConfigureAwait(false);
});
});
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == 403)
{
context.Response.ContentType = "application/json";
await context.Response.WriteAsJsonAsync(new { message = "Access forbidden. Please check your permissions or CORS configuration." });
}
});
app.UseCors("AllowFrontend");
app.UseAuthorization();
app.MapControllers();
app.Run();