-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
209 lines (171 loc) · 6.52 KB
/
Program.cs
File metadata and controls
209 lines (171 loc) · 6.52 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using Microsoft.EntityFrameworkCore;
using PortfolioCV.Data;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using PortfolioCV.Services;
using PortfolioCV.Models;
using QuestPDF.Infrastructure;
QuestPDF.Settings.License = LicenseType.Community;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddScoped<IEmailService, EmailService>();
builder.Services.AddScoped<CvService>();
// Response Compression for better performance
builder.Services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
options.Providers.Add<Microsoft.AspNetCore.ResponseCompression.BrotliCompressionProvider>();
options.Providers.Add<Microsoft.AspNetCore.ResponseCompression.GzipCompressionProvider>();
});
// Memory Cache for performance
builder.Services.AddMemoryCache();
builder.Services.AddControllersWithViews()
.AddViewLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
// Add API Controllers with JSON support
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase;
});
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
// Database - SQL Server
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
?? "Server=(localdb)\\mssqllocaldb;Database=PortfolioCV;Trusted_Connection=True;";
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(connectionString));
// JWT Configuration
var jwtKey = builder.Configuration["Jwt:Key"] ?? "YourSuperSecretKeyForJWTAuthenticationMin32Chars!";
var jwtIssuer = builder.Configuration["Jwt:Issuer"] ?? "PortfolioCV";
var jwtAudience = builder.Configuration["Jwt:Audience"] ?? "PortfolioCVAdmin";
// Authentication - Both Cookie (for existing MVC) and JWT (for new React API)
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
// Legacy cookie auth for MVC if still needed for frontend,
// but admin is now handled by JWT in React.
options.Cookie.Name = "PortfolioCV.Auth";
})
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = jwtIssuer,
ValidAudience = jwtAudience,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey))
};
});
// Authorization policies
builder.Services.AddAuthorization(options =>
{
// Default policy for API controllers - requires JWT
options.AddPolicy("ApiPolicy", policy =>
{
policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme);
policy.RequireAuthenticatedUser();
});
});
// CORS for React Admin - HARDCODED for production reliability
builder.Services.AddCors(options =>
{
options.AddPolicy("ReactAdmin", policy =>
{
policy.WithOrigins(
"http://localhost:5173",
"http://localhost:5174",
"https://dashboard.veyselmut.com.tr"
)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
builder.Services.AddSession();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
// app.UseHttpsRedirection();
app.UseResponseCompression(); // Enable compression
app.UseStaticFiles();
// Visitor Analytics Tracker - Public Page Visits
app.UseMiddleware<PortfolioCV.Middleware.VisitorTrackerMiddleware>();
// Bypass IIS Delete/Put blocking by allowing Method Override via Header X-HTTP-Method-Override
app.UseHttpMethodOverride();
app.UseRouting();
// Enable CORS
app.UseCors("ReactAdmin");
// Localization
var supportedCultures = new[] { "tr-TR", "en-US" };
var localizationOptions = new RequestLocalizationOptions()
.SetDefaultCulture("tr-TR")
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
app.UseRequestLocalization(localizationOptions);
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
// Map API routes (JWT auth)
app.MapControllers();
// Admin Panel SPA Fallback removed - admin panel is on separate domain (dashboard.veyselmut.com.tr)
// Map MVC routes (Cookie auth)
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
// Initialize database
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<AppDbContext>();
Console.WriteLine($"[STARTUP] Using Connection String: {connectionString}");
// Initialize Database
context.Database.EnsureCreated();
Console.WriteLine($"[STARTUP] Database initialized.");
// Manual Migration for Visitors Table (Bypassing EF Migration issues)
try
{
context.Database.ExecuteSqlRaw(@"
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='Visitors' AND xtype='U')
CREATE TABLE Visitors (
Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
IpAddress NVARCHAR(MAX) NOT NULL,
UserAgent NVARCHAR(MAX) NOT NULL,
Path NVARCHAR(MAX) NOT NULL,
VisitDate DATETIME2 NOT NULL
)
");
Console.WriteLine("[STARTUP] Visitors table checked/created.");
}
catch (Exception ex)
{
Console.WriteLine($"[STARTUP] Visitor Table error: {ex.Message}");
}
// Visitor seeding removed - production uses real data only
Console.WriteLine("[STARTUP] Visitors table ready.");
// Seed Contacts block removed as per user request
// if (!context.Contacts.Any()) { ... }
context.SaveChanges();
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred creating the DB.");
}
}
app.Run();
// Rebuild trigger 1