| 
									
										
										
										
											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; | 
					
						
							|  |  |  | using System.Reflection; | 
					
						
							| 
									
										
										
										
											2025-07-01 11:10:28 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 11:44:10 -03:00
										 |  |  | // Carga las variables de entorno desde el archivo .env en la raíz de la solución. | 
					
						
							|  |  |  | DotNetEnv.Env.Load(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 11:44:10 -03:00
										 |  |  |                           policy.WithOrigins("http://localhost:5173", // Desarrollo Frontend | 
					
						
							|  |  |  |                                 "http://192.168.10.78:5173", // Desarrollo en Red Local | 
					
						
							|  |  |  |                                 "https://www.eldia.com" // <--- DOMINIO DE PRODUCCIÓN | 
					
						
							|  |  |  |                                 ) | 
					
						
							| 
									
										
										
										
											2025-07-01 13:26:46 -03:00
										 |  |  |                                 .AllowAnyHeader() | 
					
						
							|  |  |  |                                 .AllowAnyMethod(); | 
					
						
							|  |  |  |                       }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 12:19:00 -03:00
										 |  |  | // 1. Registramos nuestra fábrica de conexiones a la BD. | 
					
						
							| 
									
										
										
										
											2025-07-01 11:10:28 -03:00
										 |  |  | builder.Services.AddSingleton<IDbConnectionFactory, SqlConnectionFactory>(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 12:19:00 -03:00
										 |  |  | // 2. AÑADIR: Registramos los repositorios que la API necesitará para LEER datos. | 
					
						
							|  |  |  | builder.Services.AddScoped<ICotizacionGanadoRepository, CotizacionGanadoRepository>(); | 
					
						
							|  |  |  | builder.Services.AddScoped<ICotizacionGranoRepository, CotizacionGranoRepository>(); | 
					
						
							|  |  |  | builder.Services.AddScoped<ICotizacionBolsaRepository, CotizacionBolsaRepository>(); | 
					
						
							|  |  |  | builder.Services.AddScoped<IFuenteDatoRepository, FuenteDatoRepository>(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 3. Configurar FluentMigrator | 
					
						
							| 
									
										
										
										
											2025-07-01 11:25:37 -03:00
										 |  |  | builder.Services | 
					
						
							|  |  |  |     .AddFluentMigratorCore() | 
					
						
							|  |  |  |     .ConfigureRunner(rb => rb | 
					
						
							|  |  |  |         // Usar el conector para SQL Server | 
					
						
							|  |  |  |         .AddSqlServer() | 
					
						
							|  |  |  |         // Obtener la cadena de conexión desde appsettings.json | 
					
						
							|  |  |  |         .WithGlobalConnectionString(builder.Configuration.GetConnectionString("DefaultConnection")) | 
					
						
							|  |  |  |         // Definir el ensamblado (proyecto) que contiene las migraciones | 
					
						
							|  |  |  |         .ScanIn(typeof(CreateInitialTables).Assembly).For.Migrations()) | 
					
						
							|  |  |  |     // Habilitar el logging para ver qué hacen las migraciones en la consola | 
					
						
							|  |  |  |     .AddLogging(lb => lb.AddFluentMigratorConsole()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 10:46:13 -03:00
										 |  |  | // Add services to the container. | 
					
						
							| 
									
										
										
										
											2025-07-01 11:10:28 -03:00
										 |  |  | builder.Services.AddControllers(); | 
					
						
							|  |  |  | builder.Services.AddEndpointsApiExplorer(); | 
					
						
							|  |  |  | builder.Services.AddSwaggerGen(); | 
					
						
							| 
									
										
										
										
											2025-07-01 10:46:13 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | var app = builder.Build(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 12:19:00 -03:00
										 |  |  | // 4. Ejecutar las migraciones al iniciar la aplicación (ideal para desarrollo y despliegues sencillos) | 
					
						
							| 
									
										
										
										
											2025-07-01 11:25:37 -03:00
										 |  |  | // Obtenemos el "scope" de los servicios para poder solicitar el MigrationRunner | 
					
						
							|  |  |  | using (var scope = app.Services.CreateScope()) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     var migrationRunner = scope.ServiceProvider.GetRequiredService<IMigrationRunner>(); | 
					
						
							|  |  |  |     // Ejecuta las migraciones pendientes | 
					
						
							|  |  |  |     migrationRunner.MigrateUp(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 10:46:13 -03:00
										 |  |  | // Configure the HTTP request pipeline. | 
					
						
							|  |  |  | 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(); | 
					
						
							| 
									
										
										
										
											2025-07-01 10:46:13 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 11:10:28 -03:00
										 |  |  | app.MapControllers(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | app.Run(); |