feat: Worker Service - API endpoints
Implement and configure Worker Service to orchestrate data fetchers - Implement API endpoints for stock market data
This commit is contained in:
@@ -1,7 +1,51 @@
|
||||
using Mercados.Infrastructure;
|
||||
using Mercados.Infrastructure.DataFetchers;
|
||||
using Mercados.Infrastructure.Persistence;
|
||||
using Mercados.Infrastructure.Persistence.Repositories;
|
||||
using Mercados.Worker;
|
||||
|
||||
var builder = Host.CreateApplicationBuilder(args);
|
||||
builder.Services.AddHostedService<Worker>();
|
||||
// --- Configuración del Host ---
|
||||
// Esto prepara el host del servicio, permitiendo la inyección de dependencias,
|
||||
// la configuración desde appsettings.json y el logging.
|
||||
IHost host = Host.CreateDefaultBuilder(args)
|
||||
.ConfigureServices((hostContext, services) =>
|
||||
{
|
||||
// Obtenemos la configuración desde el host builder para usarla aquí.
|
||||
IConfiguration configuration = hostContext.Configuration;
|
||||
|
||||
var host = builder.Build();
|
||||
host.Run();
|
||||
// --- 1. Registro de Servicios de Infraestructura ---
|
||||
|
||||
// Registramos la fábrica de conexiones a la BD. Es un Singleton porque
|
||||
// solo necesita ser creada una vez para leer la cadena de conexión.
|
||||
services.AddSingleton<IDbConnectionFactory, SqlConnectionFactory>();
|
||||
|
||||
// Registramos los repositorios. Se crean "por petición" (Scoped).
|
||||
// En un worker, "Scoped" significa que se creará una instancia por cada
|
||||
// ejecución del servicio, lo cual es seguro y eficiente.
|
||||
services.AddScoped<ICotizacionGanadoRepository, CotizacionGanadoRepository>();
|
||||
services.AddScoped<ICotizacionGranoRepository, CotizacionGranoRepository>();
|
||||
services.AddScoped<ICotizacionBolsaRepository, CotizacionBolsaRepository>();
|
||||
services.AddScoped<IFuenteDatoRepository, FuenteDatoRepository>();
|
||||
|
||||
// --- 2. Registro de los Data Fetchers ---
|
||||
|
||||
// Registramos CADA uno de nuestros fetchers. El contenedor de DI sabrá
|
||||
// que todos implementan la interfaz IDataFetcher.
|
||||
services.AddScoped<IDataFetcher, MercadoAgroFetcher>();
|
||||
services.AddScoped<IDataFetcher, BcrDataFetcher>();
|
||||
//services.AddScoped<IDataFetcher, FinnhubDataFetcher>();
|
||||
services.AddScoped<IDataFetcher, YahooFinanceDataFetcher>();
|
||||
|
||||
// El cliente HTTP es fundamental para hacer llamadas a APIs externas.
|
||||
// Le damos un nombre al cliente de Finnhub para cumplir con los requisitos de su constructor.
|
||||
services.AddHttpClient("Finnhub");
|
||||
|
||||
|
||||
// --- 3. Registro del Worker Principal ---
|
||||
|
||||
// Finalmente, registramos nuestro servicio de fondo (el worker en sí).
|
||||
services.AddHostedService<DataFetchingService>();
|
||||
})
|
||||
.Build();
|
||||
|
||||
await host.RunAsync();
|
||||
Reference in New Issue
Block a user