56 lines
1.9 KiB
C#
56 lines
1.9 KiB
C#
using System;
|
|
using FinlyticCore.Database;
|
|
using FinlyticCore.Services;
|
|
using FinlyticSentiment.Database;
|
|
using FinlyticSentiment.Services;
|
|
using FinlyticSentiment.Util;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
var builder = Host.CreateApplicationBuilder(args);
|
|
|
|
// Register PostgreSQL DbContext for sentiment data & settings
|
|
builder.Services.AddDbContext<SentimentDbContext>(options =>
|
|
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
|
|
builder.Services.AddScoped<ISettingsDbContext>(sp => sp.GetRequiredService<SentimentDbContext>());
|
|
|
|
// Register Core Services
|
|
builder.Services.AddSingleton<ISettingsService, SettingsService>();
|
|
builder.Services.AddSingleton(typeof(IFinlyticLogger<>), typeof(FinlyticLogger<>));
|
|
|
|
// Register HttpClient
|
|
builder.Services.AddHttpClient();
|
|
|
|
// Register Sentiment Services
|
|
builder.Services.AddScoped<ISentimentDbService, SentimentDbService>();
|
|
builder.Services.AddSingleton<IFinBertAnalyzerService, FinBertAnalyzerService>();
|
|
|
|
// 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>();
|
|
var connStr = builder.Configuration.GetConnectionString("DefaultConnection") ?? "";
|
|
await db.MigrateWithBootstrapAsync(connStr);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Critical error during database migration for FinlyticSentiment: {ex.Message}");
|
|
}
|
|
|
|
}
|
|
|
|
await host.RunAsync();
|