108 lines
3.3 KiB
C#
108 lines
3.3 KiB
C#
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
using Serilog;
|
||
|
|
using FluentValidation;
|
||
|
|
using GestorFacturas.API.Data;
|
||
|
|
using GestorFacturas.API.Services;
|
||
|
|
using GestorFacturas.API.Services.Interfaces;
|
||
|
|
using GestorFacturas.API.Workers;
|
||
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||
|
|
using Microsoft.IdentityModel.Tokens;
|
||
|
|
using System.Text;
|
||
|
|
|
||
|
|
var builder = WebApplication.CreateBuilder(args);
|
||
|
|
// ===== CONFIGURAR SERILOG (LIMPIO PARA PRODUCCIÓN) =====
|
||
|
|
Log.Logger = new LoggerConfiguration()
|
||
|
|
.ReadFrom.Configuration(builder.Configuration) // Lee filtros del appsettings
|
||
|
|
.WriteTo.Console() // ÚNICA SALIDA: Consola (para que Docker/Grafana lo recojan)
|
||
|
|
.CreateLogger();
|
||
|
|
|
||
|
|
builder.Host.UseSerilog();
|
||
|
|
// ===== SERVICIOS =====
|
||
|
|
builder.Services.AddControllers();
|
||
|
|
builder.Services.AddEndpointsApiExplorer();
|
||
|
|
builder.Services.AddSwaggerGen();
|
||
|
|
// Entity Framework
|
||
|
|
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
||
|
|
options.UseSqlServer(
|
||
|
|
builder.Configuration.GetConnectionString("DefaultConnection"),
|
||
|
|
sqlOptions => sqlOptions.EnableRetryOnFailure()
|
||
|
|
)
|
||
|
|
);
|
||
|
|
// Auth Service
|
||
|
|
builder.Services.AddScoped<AuthService>();
|
||
|
|
// Servicio de Encriptación
|
||
|
|
builder.Services.AddScoped<IEncryptionService, EncryptionService>();
|
||
|
|
// JWT Configuration
|
||
|
|
var jwtKey = builder.Configuration["Jwt:Key"] ?? throw new InvalidOperationException("JWT Key missing");
|
||
|
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||
|
|
.AddJwtBearer(options =>
|
||
|
|
{
|
||
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
||
|
|
{
|
||
|
|
ValidateIssuer = true,
|
||
|
|
ValidateAudience = true,
|
||
|
|
ValidateLifetime = true,
|
||
|
|
ValidateIssuerSigningKey = true,
|
||
|
|
ValidIssuer = builder.Configuration["Jwt:Issuer"],
|
||
|
|
ValidAudience = builder.Configuration["Jwt:Audience"],
|
||
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey))
|
||
|
|
};
|
||
|
|
});
|
||
|
|
// FluentValidation
|
||
|
|
builder.Services.AddValidatorsFromAssemblyContaining<Program>();
|
||
|
|
// Business Services
|
||
|
|
builder.Services.AddScoped<IMailService, MailService>();
|
||
|
|
builder.Services.AddScoped<IProcesadorFacturasService, ProcesadorFacturasService>();
|
||
|
|
// Background Service
|
||
|
|
builder.Services.AddHostedService<CronogramaWorker>();
|
||
|
|
// CORS
|
||
|
|
builder.Services.AddCors(options =>
|
||
|
|
{
|
||
|
|
options.AddPolicy("AllowReactApp", policy =>
|
||
|
|
{
|
||
|
|
policy.WithOrigins("http://localhost:5173", "http://localhost:3000", "http://localhost:80")
|
||
|
|
.AllowAnyMethod()
|
||
|
|
.AllowAnyHeader()
|
||
|
|
.AllowCredentials();
|
||
|
|
});
|
||
|
|
});
|
||
|
|
var app = builder.Build();
|
||
|
|
// Migraciones Automáticas
|
||
|
|
using (var scope = app.Services.CreateScope())
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||
|
|
await context.Database.MigrateAsync();
|
||
|
|
Log.Information("Base de datos migrada correctamente");
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
Log.Error(ex, "Error al aplicar migraciones de base de datos");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// Middleware Pipeline
|
||
|
|
if (app.Environment.IsDevelopment())
|
||
|
|
{
|
||
|
|
app.UseSwagger();
|
||
|
|
app.UseSwaggerUI();
|
||
|
|
}
|
||
|
|
app.UseSerilogRequestLogging();
|
||
|
|
app.UseHttpsRedirection();
|
||
|
|
app.UseCors("AllowReactApp");
|
||
|
|
app.UseAuthentication();
|
||
|
|
app.UseAuthorization();
|
||
|
|
app.MapControllers();
|
||
|
|
try
|
||
|
|
{
|
||
|
|
Log.Information("Aplicación Gestor de Facturas iniciada");
|
||
|
|
app.Run();
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
Log.Fatal(ex, "La aplicación terminó inesperadamente");
|
||
|
|
}
|
||
|
|
finally
|
||
|
|
{
|
||
|
|
Log.CloseAndFlush();
|
||
|
|
}
|