-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
131 lines (110 loc) · 4.34 KB
/
Program.cs
File metadata and controls
131 lines (110 loc) · 4.34 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
using CoreXCrud.Data;
using CoreXCrud.Repositories;
using Microsoft.EntityFrameworkCore;
using AutoMapper;
using FluentValidation;
using FluentValidation.AspNetCore;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using System.Text;
using AspNetCoreRateLimit;
using Serilog;
using CoreXCrud.Mappings;
using CoreXCrud.Validators.UserValidators;
var builder = WebApplication.CreateBuilder(args);
// 📌 1️⃣ Serilog Loglama Konfigürasyonu
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File("logs/log-.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
builder.Host.UseSerilog();
// 📌 2️⃣ Veritabanı Bağlantısını Yapılandırma
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))
);
// 📌 3️⃣ Repository & Unit of Work Bağımlılıklarını Ekleyelim
builder.Services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
// 📌 4️⃣ AutoMapper ve FluentValidation Servislerini Ekleyelim
builder.Services.AddAutoMapper(typeof(MappingProfile));
builder.Services.AddControllers().AddFluentValidation(fv =>
{
fv.RegisterValidatorsFromAssemblyContaining<UserValidator>();
});
// 📌 5️⃣ Rate Limiting Servislerini Ekleyelim
builder.Services.AddMemoryCache();
builder.Services.Configure<IpRateLimitOptions>(builder.Configuration.GetSection("IpRateLimiting"));
builder.Services.Configure<IpRateLimitPolicies>(builder.Configuration.GetSection("IpRateLimitPolicies"));
builder.Services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
builder.Services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
builder.Services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
builder.Services.AddSingleton<IProcessingStrategy, AsyncKeyLockProcessingStrategy>();
// 📌 6️⃣ JWT Authentication’ı Ekleyelim
var jwtSettings = builder.Configuration.GetSection("JwtSettings");
var secretKey = Encoding.UTF8.GetBytes(jwtSettings["Secret"]);
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = jwtSettings["Issuer"],
ValidAudience = jwtSettings["Audience"],
IssuerSigningKey = new SymmetricSecurityKey(secretKey)
};
});
builder.Services.AddAuthorization();
// 📌 7️⃣ Swagger (API Dokümantasyonu) ve Yetkilendirme Ayarlarını Ekleyelim
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "CoreXCrud API", Version = "v1" });
// 📌 Swagger UI için JWT Yetkilendirme Butonu Ekleyelim
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Name = "Authorization",
Type = SecuritySchemeType.Http,
Scheme = "Bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description = "JWT Token formatında 'Bearer {token}' şeklinde giriniz."
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] {}
}
});
});
var app = builder.Build();
// 📌 8️⃣ Geliştirme Ortamında Swagger UI’yi Aç
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
// 📌 9️⃣ HTTPS Yönlendirme
app.UseHttpsRedirection();
// 📌 🔟 Yetkilendirme Mekanizmasını Kullan
app.UseAuthentication();
app.UseAuthorization();
// 📌 1️⃣1️⃣ Rate Limiting Middleware’i Aktif Hale Getir
app.UseIpRateLimiting();
// 📌 1️⃣2️⃣ Serilog Middleware’i Aktif Hale Getir
app.UseSerilogRequestLogging();
// 📌 1️⃣3️⃣ Controller Endpoint'lerini Tanımla
app.MapControllers();
// 📌 1️⃣4️⃣ Uygulamayı Başlat
app.Run();