Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 53 additions & 7 deletions Presentation/ASPNET/Program.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,48 @@
using ASPNET.BackEnd;
using ASPNET.BackEnd.Common.Middlewares;
using ASPNET.FrontEnd;
using Serilog;

var builder = WebApplication.CreateBuilder(args);

//>>> Create Logs folder for Serilog
//>>> Configure Serilog logging
var logPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "app_data", "logs");
if (!Directory.Exists(logPath))
try
{
Directory.CreateDirectory(logPath);
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())
Expand All @@ -25,13 +52,32 @@
}

app.UseRouting();
app.UseCors();
app.UseMiddleware<GlobalApiExceptionHandlerMiddleware>();

// Apply CORS policy
app.UseCors("AllowAll");

// Authentication & Authorization
app.UseAuthentication();
app.UseAuthorization();
app.MapStaticAssets();

// Global exception middleware should be after auth
app.UseMiddleware<GlobalApiExceptionHandlerMiddleware>();

// Map static assets and frontend/backend routes
app.MapStaticAssets();
app.MapFrontEndRoutes();
app.MapBackEndRoutes();

app.Run();
// Safe startup
try
{
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Application failed to start");
}
finally
{
Log.CloseAndFlush();
}