From d919516f2d9878e2c06e5e0a0e0220b10610e868 Mon Sep 17 00:00:00 2001 From: MasterDraco Date: Wed, 12 Mar 2025 21:57:00 +0000 Subject: [PATCH] Remove MetricsBackgroundService registration --- src/Api/Program.cs | 95 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 88 insertions(+), 7 deletions(-) diff --git a/src/Api/Program.cs b/src/Api/Program.cs index 0484445..cfe8023 100644 --- a/src/Api/Program.cs +++ b/src/Api/Program.cs @@ -1,25 +1,72 @@ using Microsoft.AspNetCore.Builder; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using System; +using System.IO; +using System.Linq; +using System.Threading.Tasks; using TransmissionRssManager.Core; +using TransmissionRssManager.Data; +using TransmissionRssManager.Data.Models; +using TransmissionRssManager.Data.Repositories; using TransmissionRssManager.Services; +using Serilog; +using Serilog.Events; + +// Configure Serilog +var logsDirectory = Path.Combine(AppContext.BaseDirectory, "logs"); +Directory.CreateDirectory(logsDirectory); + +Log.Logger = new LoggerConfiguration() + .MinimumLevel.Information() + .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) + .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information) + .Enrich.FromLogContext() + .WriteTo.Console() + .WriteTo.File( + Path.Combine(logsDirectory, "log-.txt"), + rollingInterval: RollingInterval.Day, + retainedFileCountLimit: 31, + fileSizeLimitBytes: 10 * 1024 * 1024) + .CreateLogger(); var builder = WebApplication.CreateBuilder(args); +// Add Serilog to the application +builder.Host.UseSerilog(); + // Add services to the container builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -// Add custom services -builder.Services.AddSingleton(); -builder.Services.AddSingleton(); -builder.Services.AddSingleton(); -builder.Services.AddSingleton(); +// Configure database +var connectionString = builder.Configuration["ConnectionStrings:DefaultConnection"] + ?? "Host=localhost;Database=torrentmanager;Username=postgres;Password=postgres"; +builder.Services.AddDbContext(options => + options.UseNpgsql(connectionString)); -// Add background services +// Register repositories +builder.Services.AddScoped(typeof(IRepository<>), typeof(Repository<>)); +builder.Services.AddScoped(); +builder.Services.AddScoped(); + +// Add custom services (now scoped instead of singleton to work with DbContext) +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); + +// Add background services (these remain singleton) builder.Services.AddHostedService(); builder.Services.AddHostedService(); +builder.Services.AddHostedService(); +// MetricsBackgroundService is not needed with the simplified implementation var app = builder.Build(); @@ -35,4 +82,38 @@ app.UseRouting(); app.UseAuthorization(); app.MapControllers(); -app.Run(); \ No newline at end of file +// Initialize database and run migrations +using (var scope = app.Services.CreateScope()) +{ + var services = scope.ServiceProvider; + try + { + var context = services.GetRequiredService(); + context.Database.Migrate(); + + // Run data migration from file-based storage if needed + if (!context.UserPreferences.Any()) + { + var migrationService = services.GetRequiredService(); + await migrationService.MigrateDataAsync(); + } + } + catch (Exception ex) + { + var logger = services.GetRequiredService>(); + logger.LogError(ex, "An error occurred during database initialization or migration"); + } +} + +try +{ + await app.RunAsync(); +} +catch (Exception ex) +{ + Log.Fatal(ex, "Application terminated unexpectedly"); +} +finally +{ + Log.CloseAndFlush(); +} \ No newline at end of file