-
Notifications
You must be signed in to change notification settings - Fork 660
Expand file tree
/
Copy pathProgram.cs
More file actions
83 lines (68 loc) · 1.74 KB
/
Program.cs
File metadata and controls
83 lines (68 loc) · 1.74 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
using ASPNET.BackEnd;
using ASPNET.BackEnd.Common.Middlewares;
using ASPNET.FrontEnd;
using Serilog;
var builder = WebApplication.CreateBuilder(args);
//>>> Configure Serilog logging
var logPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "app_data", "logs");
try
{
if (!Directory.Exists(logPath))
{
Directory.CreateDirectory(logPath);
}
}
catch (Exception ex)
{
Console.WriteLine($"Failed to create logs directory: {ex.Message}");
}
// Configure Serilog
Log.Logger = new LoggerConfiguration()
.WriteTo.File(Path.Combine(logPath, "app.log"), rollingInterval: RollingInterval.Day)
.CreateLogger();
builder.Host.UseSerilog();
builder.Services.AddBackEndServices(builder.Configuration);
builder.Services.AddFrontEndServices();
// Configure CORS policy
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
{
policy.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
var app = builder.Build();
// Register backend services and routes
app.RegisterBackEndBuilder(app.Environment, app, builder.Configuration);
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseRouting();
// Apply CORS policy
app.UseCors("AllowAll");
// Authentication & Authorization
app.UseAuthentication();
app.UseAuthorization();
// Global exception middleware should be after auth
app.UseMiddleware<GlobalApiExceptionHandlerMiddleware>();
// Map static assets and frontend/backend routes
app.MapStaticAssets();
app.MapFrontEndRoutes();
app.MapBackEndRoutes();
// Safe startup
try
{
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Application failed to start");
}
finally
{
Log.CloseAndFlush();
}