Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
159 lines (133 loc) · 5.07 KB
/
Copy pathProgram.cs
File metadata and controls
159 lines (133 loc) · 5.07 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System.Text.Json;
using System.Threading.RateLimiting;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using TaskManagementApi.Features.Tasks;
using TaskManagementApi.Features.Tasks.Models;
using TaskManagementApi.Shared.Database;
using TaskManagementApi.Shared.Middleware;
using TaskManagementApi.Shared.DTOs;
using TaskManagementApi.Features.Tasks.Validators;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
// Include XML comments if the file is present
var xmlFile = Path.ChangeExtension(builder.Environment.ApplicationName, ".xml");
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
if (File.Exists(xmlPath))
{
options.IncludeXmlComments(xmlPath);
}
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Task Management API",
Version = "v1",
Description = "API for managing tasks - demo"
});
});
// Configure Entity Framework with In-Memory Database
builder.Services.AddDbContext<TaskManagementContext>(options => options.UseInMemoryDatabase("TaskManagementDb"));
// Application services
builder.Services.AddScoped<ITaskRepository, TaskRepository>();
builder.Services.AddScoped<ITaskService, TaskService>();
builder.Services.AddScoped<TaskProvider>();
// Add any additional services needed
builder.Services.AddScoped<FutureDateAttribute>();
builder.Services.AddScoped<PriorityAttribute>();
// Configure rate limiting
builder.Services.AddRateLimiter(options =>
{
options.GlobalLimiter = PartitionedRateLimiter.Create<HttpContext, string>(httpContext =>
{
var clientIp = httpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown";
return RateLimitPartition.GetFixedWindowLimiter(clientIp, _ => new FixedWindowRateLimiterOptions
{
PermitLimit =60,
Window = TimeSpan.FromMinutes(1),
QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
QueueLimit =0
});
});
options.OnRejected = async (context, ct) =>
{
context.HttpContext.Response.StatusCode = StatusCodes.Status429TooManyRequests;
context.HttpContext.Response.ContentType = "application/json";
var payload = new ErrorResponse
{
Message = "Too many requests. Please try again later.",
TraceId = context.HttpContext.TraceIdentifier
};
var json = JsonSerializer.Serialize(payload, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
await context.HttpContext.Response.WriteAsync(json, ct);
};
});
// Configure CORS if needed
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policy =>
{
policy.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
// Add global exception handling middleware
app.UseMiddleware<GlobalExceptionHandlerMiddleware>();
// Enable rate limiting middleware
app.UseRateLimiter();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Task Management API v1");
// Expand the documentation by default
options.DefaultModelsExpandDepth(-1);
options.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.List);
});
// Seed some sample data in development
using (var scope = app.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<TaskManagementContext>();
if (!context.Tasks.Any())
{
var now = DateTime.UtcNow;
context.Tasks.AddRange(
new TaskItem
{
Id = Guid.NewGuid(),
Title = "Refine onboarding flow",
Description = "Audit the onboarding experience and capture top three fixes.",
Priority = TaskPriority.High,
Status = TaskItemStatus.InProgress,
DueDate = now.AddDays(7),
CreatedAt = now.AddDays(-2),
UpdatedAt = now.AddDays(-1),
Tags = new List<string> { "product", "ux" }
},
new TaskItem
{
Id = Guid.NewGuid(),
Title = "Document API guidelines",
Description = "Draft the initial version of our HTTP API style guide.",
Priority = TaskPriority.Medium,
Status = TaskItemStatus.Pending,
DueDate = now.AddDays(14),
CreatedAt = now.AddDays(-3),
UpdatedAt = now.AddDays(-3),
Tags = new List<string> { "docs" }
}
);
await context.SaveChangesAsync();
}
}
}
app.UseHttpsRedirection();
app.UseCors();
app.UseAuthorization();
app.MapControllers();
app.Run();
public partial class Program { }