45 lines
1.5 KiB
C#
45 lines
1.5 KiB
C#
using FinlyticSentiment.Database;
|
|
using FinlyticSentiment.Services;
|
|
using FinlyticSentiment.Util;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
var builder = Host.CreateApplicationBuilder(args);
|
|
|
|
// Register PostgreSQL DbContext for settings persistence
|
|
builder.Services.AddDbContext<SentimentDbContext>(options =>
|
|
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
|
|
|
|
// Register HttpClient
|
|
builder.Services.AddHttpClient();
|
|
|
|
// Register Service interfaces and co-located implementations
|
|
builder.Services.AddScoped<ISettingsDbService, SettingsDbService>();
|
|
builder.Services.AddSingleton<IFinBertAnalyzerService, FinBertAnalyzerService>();
|
|
builder.Services.AddSingleton<ISentimentStorageService, SentimentStorageService>();
|
|
|
|
// Register MQTT Client (as singleton hosted service)
|
|
builder.Services.AddSingleton<SentimentMqttClient>();
|
|
builder.Services.AddHostedService(sp => sp.GetRequiredService<SentimentMqttClient>());
|
|
|
|
// Register Sentiment Background Worker
|
|
builder.Services.AddHostedService<SentimentBackgroundService>();
|
|
|
|
var host = builder.Build();
|
|
|
|
// Auto-migrate database on startup
|
|
using (var scope = host.Services.CreateScope())
|
|
{
|
|
try
|
|
{
|
|
var db = scope.ServiceProvider.GetRequiredService<SentimentDbContext>();
|
|
await db.Database.MigrateAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
|
|
logger.LogError(ex, "An error occurred during database migration for FinlyticSentiment on startup.");
|
|
}
|
|
}
|
|
|
|
await host.RunAsync();
|