Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
77 lines (64 loc) · 2.37 KB
/
Copy pathProgram.cs
File metadata and controls
77 lines (64 loc) · 2.37 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
using ApiProtection.Data;
using ApiProtection.Middleware;
using ApiProtection.Services;
using ApiProtection.Services.Interface;
using Microsoft.EntityFrameworkCore;
using StackExchange.Redis;
var builder = WebApplication.CreateBuilder(args);
// -----------------------------
// DATABASE
// -----------------------------
var connectionString =
builder.Configuration.GetConnectionString("DefaultConnection")
?? throw new InvalidOperationException(
"Connection string 'DefaultConnection' not found. Add it to appsettings.json."
);
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(connectionString)
);
// -----------------------------
// CONTROLLERS & SWAGGER
// -----------------------------
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// -----------------------------
// REDIS (ONLY USED BY MIDDLEWARE)
// -----------------------------
var redisConfiguration = ConfigurationOptions.Parse("localhost:6379");
redisConfiguration.AbortOnConnectFail = false;
redisConfiguration.ConnectRetry = 3;
redisConfiguration.ConnectTimeout = 5000;
builder.Services.AddSingleton<IConnectionMultiplexer>(
ConnectionMultiplexer.Connect(redisConfiguration)
);
// -----------------------------
// SQL SERVICES (SP CALLERS)
// -----------------------------
builder.Services.AddScoped<IApiKeySqlService, ApiKeySqlService>();
builder.Services.AddScoped<IBlockSqlService, BlockSqlService>();
builder.Services.AddScoped<IRatePolicySqlService, RatePolicySqlService>();
builder.Services.AddScoped<IRequestLogSqlService, RequestLogSqlService>();
builder.Services.AddScoped<IAdminBlockSqlService, AdminBlockSqlService>();
builder.Services.AddScoped<IAdminAnalyticsSqlService, AdminAnalyticsSqlService>();
// -----------------------------
// BUILD APP
// -----------------------------
var app = builder.Build();
// -----------------------------
// HTTP PIPELINE
// -----------------------------
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
c.SwaggerEndpoint("/swagger/v1/swagger.json", "ApiProtection v1")
);
}
// 🔒 RATE LIMIT & API PROTECTION (MUST BE BEFORE CONTROLLERS)
app.UseMiddleware<RateLimitMiddleware>();
app.UseMiddleware<GlobalExceptionMiddleware>();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();