Files
Mercados-Web/src/Mercados.Api/Program.cs

88 lines
3.1 KiB
C#
Raw Normal View History

using FluentMigrator.Runner;
using Mercados.Database.Migrations;
using Mercados.Infrastructure;
using Mercados.Infrastructure.Persistence;
using Mercados.Infrastructure.Persistence.Repositories;
2025-07-03 15:56:06 -03:00
using Mercados.Api.Utils;
2025-07-04 16:06:13 -03:00
using Microsoft.AspNetCore.HttpOverrides;
var builder = WebApplication.CreateBuilder(args);
// Nombre para política de CORS
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
// Añadimos el servicio de CORS
builder.Services.AddCors(options =>
{
options.AddPolicy(name: MyAllowSpecificOrigins,
policy =>
{
policy.WithOrigins("http://localhost:5173",
"http://192.168.10.78:5173",
2025-07-04 14:17:29 -03:00
"https://www.eldia.com",
"https://extras.eldia.com")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
// Registros de servicios (esto está perfecto)
builder.Services.AddSingleton<IDbConnectionFactory, SqlConnectionFactory>();
builder.Services.AddScoped<ICotizacionGanadoRepository, CotizacionGanadoRepository>();
builder.Services.AddScoped<ICotizacionGranoRepository, CotizacionGranoRepository>();
builder.Services.AddScoped<ICotizacionBolsaRepository, CotizacionBolsaRepository>();
builder.Services.AddScoped<IFuenteDatoRepository, FuenteDatoRepository>();
// Configuración de FluentMigrator (perfecto)
builder.Services
.AddFluentMigratorCore()
.ConfigureRunner(rb => rb
.AddSqlServer()
.WithGlobalConnectionString(builder.Configuration.GetConnectionString("DefaultConnection"))
.ScanIn(typeof(CreateInitialTables).Assembly).For.Migrations())
.AddLogging(lb => lb.AddFluentMigratorConsole());
2025-07-03 15:56:06 -03:00
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
// Añadimos nuestro convertidor personalizado para manejar las fechas.
options.JsonSerializerOptions.Converters.Add(new UtcDateTimeConverter());
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
2025-07-04 16:06:13 -03:00
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
// En un entorno de producción real, deberías limitar esto a las IPs de tus proxies.
// options.KnownProxies.Add(IPAddress.Parse("192.168.5.X")); // IP de tu NPM
});
var app = builder.Build();
2025-07-04 16:06:13 -03:00
// Le decimos a la aplicación que USE el middleware de cabeceras de reenvío.
// ¡El orden importa! Debe ir antes de UseHttpsRedirection y UseCors.
app.UseForwardedHeaders();
// Ejecución de migraciones (perfecto)
using (var scope = app.Services.CreateScope())
{
var migrationRunner = scope.ServiceProvider.GetRequiredService<IMigrationRunner>();
migrationRunner.MigrateUp();
}
// Pipeline de HTTP (perfecto)
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseCors(MyAllowSpecificOrigins);
app.UseAuthorization();
app.MapControllers();
app.Run();