| 
									
										
										
										
											2025-07-01 12:19:00 -03:00
										 |  |  | using FluentMigrator.Runner; | 
					
						
							|  |  |  | using Mercados.Database.Migrations; | 
					
						
							| 
									
										
										
										
											2025-07-01 11:10:28 -03:00
										 |  |  | using Mercados.Infrastructure; | 
					
						
							|  |  |  | using Mercados.Infrastructure.Persistence; | 
					
						
							| 
									
										
										
										
											2025-07-01 12:19:00 -03:00
										 |  |  | 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; | 
					
						
							| 
									
										
										
										
											2025-07-03 11:44:10 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 10:46:13 -03:00
										 |  |  | var builder = WebApplication.CreateBuilder(args); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 13:26:46 -03:00
										 |  |  | // Nombre para política de CORS | 
					
						
							|  |  |  | var MyAllowSpecificOrigins = "_myAllowSpecificOrigins"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Añadimos el servicio de CORS | 
					
						
							|  |  |  | builder.Services.AddCors(options => | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     options.AddPolicy(name: MyAllowSpecificOrigins, | 
					
						
							|  |  |  |                       policy => | 
					
						
							|  |  |  |                       { | 
					
						
							| 
									
										
										
										
											2025-07-03 15:55:48 -03:00
										 |  |  |                           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") | 
					
						
							| 
									
										
										
										
											2025-07-01 13:26:46 -03:00
										 |  |  |                                 .AllowAnyHeader() | 
					
						
							|  |  |  |                                 .AllowAnyMethod(); | 
					
						
							|  |  |  |                       }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 15:55:48 -03:00
										 |  |  | // Registros de servicios (esto está perfecto) | 
					
						
							| 
									
										
										
										
											2025-07-01 11:10:28 -03:00
										 |  |  | builder.Services.AddSingleton<IDbConnectionFactory, SqlConnectionFactory>(); | 
					
						
							| 
									
										
										
										
											2025-07-01 12:19:00 -03:00
										 |  |  | builder.Services.AddScoped<ICotizacionGanadoRepository, CotizacionGanadoRepository>(); | 
					
						
							|  |  |  | builder.Services.AddScoped<ICotizacionGranoRepository, CotizacionGranoRepository>(); | 
					
						
							|  |  |  | builder.Services.AddScoped<ICotizacionBolsaRepository, CotizacionBolsaRepository>(); | 
					
						
							|  |  |  | builder.Services.AddScoped<IFuenteDatoRepository, FuenteDatoRepository>(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 15:55:48 -03:00
										 |  |  | // Configuración de FluentMigrator (perfecto) | 
					
						
							| 
									
										
										
										
											2025-07-01 11:25:37 -03:00
										 |  |  | 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()); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 11:10:28 -03:00
										 |  |  | builder.Services.AddEndpointsApiExplorer(); | 
					
						
							|  |  |  | builder.Services.AddSwaggerGen(); | 
					
						
							| 
									
										
										
										
											2025-07-01 10:46:13 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 10:46:13 -03:00
										 |  |  | 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(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 15:55:48 -03:00
										 |  |  | // Ejecución de migraciones (perfecto) | 
					
						
							| 
									
										
										
										
											2025-07-01 11:25:37 -03:00
										 |  |  | using (var scope = app.Services.CreateScope()) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     var migrationRunner = scope.ServiceProvider.GetRequiredService<IMigrationRunner>(); | 
					
						
							|  |  |  |     migrationRunner.MigrateUp(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 15:55:48 -03:00
										 |  |  | // Pipeline de HTTP (perfecto) | 
					
						
							| 
									
										
										
										
											2025-07-01 10:46:13 -03:00
										 |  |  | if (app.Environment.IsDevelopment()) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-07-01 11:10:28 -03:00
										 |  |  |     app.UseSwagger(); | 
					
						
							|  |  |  |     app.UseSwaggerUI(); | 
					
						
							| 
									
										
										
										
											2025-07-01 10:46:13 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | app.UseHttpsRedirection(); | 
					
						
							| 
									
										
										
										
											2025-07-01 13:26:46 -03:00
										 |  |  | app.UseCors(MyAllowSpecificOrigins); | 
					
						
							| 
									
										
										
										
											2025-07-01 11:10:28 -03:00
										 |  |  | app.UseAuthorization(); | 
					
						
							|  |  |  | app.MapControllers(); | 
					
						
							|  |  |  | app.Run(); |