| 
									
										
										
										
											2025-07-03 15:55:48 -03:00
										 |  |  | using DotNetEnv; | 
					
						
							| 
									
										
										
										
											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:55:48 -03:00
										 |  |  | using DotNetEnv.Configuration; | 
					
						
							| 
									
										
										
										
											2025-07-01 11:10:28 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 15:55:48 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | var envFilePath = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../../../.env")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Cargamos el archivo .env desde la ruta explícita. | 
					
						
							|  |  |  | if (!Env.Load(envFilePath).Any()) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Console.WriteLine($"ADVERTENCIA: No se pudo encontrar el archivo .env en la ruta: {envFilePath}"); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											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", | 
					
						
							|  |  |  |                                              "https://www.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:55:48 -03:00
										 |  |  | // Servicios del contenedor estándar (perfecto) | 
					
						
							| 
									
										
										
										
											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-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(); |