using System.Text.Json.Serialization; using Cleanuparr.Infrastructure.Features.DownloadRemover.Consumers; using Cleanuparr.Infrastructure.Features.Notifications.Consumers; using Cleanuparr.Infrastructure.Health; using Cleanuparr.Infrastructure.Http; using Cleanuparr.Infrastructure.Http.DynamicHttpClientSystem; using Data.Models.Arr; using Infrastructure.Verticals.Notifications.Models; using MassTransit; using Microsoft.Extensions.Caching.Memory; namespace Cleanuparr.Api.DependencyInjection; public static class MainDI { public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) => services .AddLogging(builder => builder.ClearProviders().AddConsole()) .AddHttpClients(configuration) .AddSingleton() .AddSingleton(serviceProvider => serviceProvider.GetRequiredService()) .AddServices() .AddHealthServices() .AddQuartzServices(configuration) .AddNotifications(configuration) .AddMassTransit(config => { config.AddConsumer>(); config.AddConsumer>(); config.AddConsumer>(); config.AddConsumer>(); config.AddConsumer>(); config.AddConsumer>(); config.AddConsumer>(); config.AddConsumer>(); config.UsingInMemory((context, cfg) => { cfg.ConfigureJsonSerializerOptions(options => { options.Converters.Add(new JsonStringEnumConverter()); options.ReferenceHandler = ReferenceHandler.IgnoreCycles; return options; }); cfg.ReceiveEndpoint("download-remover-queue", e => { e.ConfigureConsumer>(context); e.ConfigureConsumer>(context); e.ConcurrentMessageLimit = 1; e.PrefetchCount = 1; }); cfg.ReceiveEndpoint("notification-queue", e => { e.ConfigureConsumer>(context); e.ConfigureConsumer>(context); e.ConfigureConsumer>(context); e.ConfigureConsumer>(context); e.ConfigureConsumer>(context); e.ConfigureConsumer>(context); e.ConcurrentMessageLimit = 1; e.PrefetchCount = 1; }); }); }); private static IServiceCollection AddHttpClients(this IServiceCollection services, IConfiguration configuration) { // Add the dynamic HTTP client system - this replaces all the previous static configurations services.AddDynamicHttpClients(); // Add the dynamic HTTP client provider that uses the new system services.AddSingleton(); return services; } /// /// Adds health check services to the service collection /// private static IServiceCollection AddHealthServices(this IServiceCollection services) => services // Register the health check service .AddSingleton() // Register the background service for periodic health checks .AddHostedService(); }