source: Add rules for AI Coding
This commit is contained in:
@@ -1,75 +1,89 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using MyNewProjectName.Application;
|
||||
using MyNewProjectName.Infrastructure;
|
||||
using MyNewProjectName.Infrastructure.Extensions;
|
||||
using MyNewProjectName.WebAPI.Middleware;
|
||||
using Serilog;
|
||||
using MyNewProjectName.WebAPI.Extensions;
|
||||
using MyNewProjectName.Application.Interfaces;
|
||||
using MyNewProjectName.WebAPI.Services;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Configure Serilog
|
||||
builder.Host.UseSerilogLogging(builder.Configuration);
|
||||
|
||||
// Add OpenTelemetry distributed tracing
|
||||
builder.Services.AddOpenTelemetryTracing("MyNewProjectName.WebAPI");
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddControllers();
|
||||
|
||||
// Add Application Layer
|
||||
builder.Services.AddApplication();
|
||||
|
||||
// Add Infrastructure Layer
|
||||
builder.Services.AddInfrastructure(builder.Configuration);
|
||||
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
// Add Swagger
|
||||
// ==========================================
|
||||
// 1. ADD SERVICES TO CONTAINER
|
||||
// ==========================================
|
||||
builder.Services.AddControllers()
|
||||
.AddJsonOptions(options =>
|
||||
{
|
||||
// Configure enum serialization to use string representation
|
||||
options.JsonSerializerOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter());
|
||||
});
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddMemoryCache();
|
||||
builder.Services.AddLogging();
|
||||
|
||||
// Extension Methods: Swagger, JWT Authentication & Authorization
|
||||
builder.Services.AddCustomSwagger();
|
||||
builder.Services.AddJwtAuthentication(builder.Configuration);
|
||||
builder.Services.AddCustomAuthorization();
|
||||
|
||||
// Layers Registration
|
||||
builder.Services.AddApplication();
|
||||
builder.Services.AddInfrastructure(builder.Configuration);
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
builder.Services.AddScoped<ICurrentUserService, CurrentUserService>();
|
||||
|
||||
// CORS Configuration
|
||||
builder.Services.AddCors(option =>
|
||||
{
|
||||
option.AddPolicy(name: "CorsPolicy",
|
||||
configurePolicy: builder => builder
|
||||
.SetIsOriginAllowed((host) => true)
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader()
|
||||
.AllowCredentials());
|
||||
});
|
||||
|
||||
|
||||
builder.WebHost.UseUrls("http://0.0.0.0:5044");
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(option =>
|
||||
{
|
||||
option.SwaggerEndpoint("/swagger/v1/swagger.json", "iYHCT360 API");
|
||||
option.RoutePrefix = "swagger"; // UI tại /swagger
|
||||
option.DisplayRequestDuration();
|
||||
option.EnableFilter();
|
||||
option.EnableValidator();
|
||||
option.EnableTryItOutByDefault();
|
||||
});
|
||||
}
|
||||
|
||||
// Middleware pipeline order is critical:
|
||||
// 1. CorrelationIdMiddleware - Must be first to track all requests
|
||||
|
||||
// Middleware Pipeline - Order is important!
|
||||
// 1. CorrelationIdMiddleware: Generate correlation ID for request tracking (must be first)
|
||||
app.UseMiddleware<CorrelationIdMiddleware>();
|
||||
|
||||
// 2. RequestResponseLoggingMiddleware - Optional, enable only when needed
|
||||
// WARNING: This can generate large log files. Enable only for specific environments.
|
||||
// Configure in appsettings.json: "Logging:EnableRequestLogging" and "Logging:EnableResponseLogging"
|
||||
app.UseMiddleware<RequestResponseLoggingMiddleware>();
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
// 3. Authentication (built-in)
|
||||
app.UseAuthentication();
|
||||
|
||||
// 4. Authorization (built-in)
|
||||
app.UseAuthorization();
|
||||
|
||||
// 6. ExceptionHandlingMiddleware - Must be last to catch all exceptions
|
||||
// 2. ExceptionHandlingMiddleware: Global exception handler (must be early to catch all exceptions)
|
||||
app.UseMiddleware<ExceptionHandlingMiddleware>();
|
||||
|
||||
// 3. Routing & Static Files
|
||||
app.UseRouting();
|
||||
app.UseStaticFiles();
|
||||
|
||||
// 4. CORS (must be before UseAuthentication/UseAuthorization)
|
||||
app.UseCors("CorsPolicy");
|
||||
|
||||
// 5. Authentication & Authorization
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
// 6. RequestResponseLoggingMiddleware: Log request/response (after routing and auth to avoid interfering with response)
|
||||
app.UseMiddleware<RequestResponseLoggingMiddleware>();
|
||||
|
||||
// 7. Map Controllers
|
||||
app.MapControllers();
|
||||
|
||||
try
|
||||
{
|
||||
Log.Information("Starting MyNewProjectName.WebAPI");
|
||||
app.Run();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Fatal(ex, "Application terminated unexpectedly");
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Log.CloseAndFlush();
|
||||
}
|
||||
|
||||
app.Run();
|
||||
|
||||
Reference in New Issue
Block a user