feat(Worker): Implementa servicio de notificación para alertas de fallos críticos - Se remueve .env y se utilizan appsettings.Development.json y User Secrets
This commit is contained in:
		| @@ -1,12 +1,19 @@ | ||||
| using DotNetEnv; | ||||
| using FluentMigrator.Runner; | ||||
| using Mercados.Database.Migrations; | ||||
| using Mercados.Infrastructure; | ||||
| using Mercados.Infrastructure.Persistence; | ||||
| using Mercados.Infrastructure.Persistence.Repositories; | ||||
| using System.Reflection; | ||||
| using DotNetEnv.Configuration; | ||||
|  | ||||
| // Carga las variables de entorno desde el archivo .env en la raíz de la solución. | ||||
| DotNetEnv.Env.Load(); | ||||
|  | ||||
| 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}"); | ||||
| } | ||||
|  | ||||
| var builder = WebApplication.CreateBuilder(args); | ||||
|  | ||||
| @@ -19,55 +26,45 @@ builder.Services.AddCors(options => | ||||
|     options.AddPolicy(name: MyAllowSpecificOrigins, | ||||
|                       policy => | ||||
|                       { | ||||
|                           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 | ||||
|                                 ) | ||||
|                           policy.WithOrigins("http://localhost:5173", | ||||
|                                              "http://192.168.10.78:5173", | ||||
|                                              "https://www.eldia.com") | ||||
|                                 .AllowAnyHeader() | ||||
|                                 .AllowAnyMethod(); | ||||
|                       }); | ||||
| }); | ||||
|  | ||||
| // 1. Registramos nuestra fábrica de conexiones a la BD. | ||||
| // Registros de servicios (esto está perfecto) | ||||
| builder.Services.AddSingleton<IDbConnectionFactory, SqlConnectionFactory>(); | ||||
|  | ||||
| // 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 | ||||
| // Configuración de FluentMigrator (perfecto) | ||||
| 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()); | ||||
|  | ||||
|  | ||||
| // Add services to the container. | ||||
| // Servicios del contenedor estándar (perfecto) | ||||
| builder.Services.AddControllers(); | ||||
| builder.Services.AddEndpointsApiExplorer(); | ||||
| builder.Services.AddSwaggerGen(); | ||||
|  | ||||
| var app = builder.Build(); | ||||
|  | ||||
| // 4. Ejecutar las migraciones al iniciar la aplicación (ideal para desarrollo y despliegues sencillos) | ||||
| // Obtenemos el "scope" de los servicios para poder solicitar el MigrationRunner | ||||
| // Ejecución de migraciones (perfecto) | ||||
| using (var scope = app.Services.CreateScope()) | ||||
| { | ||||
|     var migrationRunner = scope.ServiceProvider.GetRequiredService<IMigrationRunner>(); | ||||
|     // Ejecuta las migraciones pendientes | ||||
|     migrationRunner.MigrateUp(); | ||||
| } | ||||
|  | ||||
| // Configure the HTTP request pipeline. | ||||
| // Pipeline de HTTP (perfecto) | ||||
| if (app.Environment.IsDevelopment()) | ||||
| { | ||||
|     app.UseSwagger(); | ||||
| @@ -75,11 +72,7 @@ if (app.Environment.IsDevelopment()) | ||||
| } | ||||
|  | ||||
| app.UseHttpsRedirection(); | ||||
|  | ||||
| app.UseCors(MyAllowSpecificOrigins); | ||||
|  | ||||
| app.UseAuthorization(); | ||||
|  | ||||
| app.MapControllers(); | ||||
|  | ||||
| app.Run(); | ||||
		Reference in New Issue
	
	Block a user