using System.Text.Json.Serialization; using Cleanuparr.Domain.Entities.Arr; using Cleanuparr.Infrastructure.Features.DownloadRemover.Consumers; using Cleanuparr.Infrastructure.Features.Notifications.Consumers; using Cleanuparr.Infrastructure.Features.Notifications.Models; using Cleanuparr.Infrastructure.Health; using Cleanuparr.Infrastructure.Http; using Cleanuparr.Infrastructure.Http.DynamicHttpClientSystem; using Data.Models.Arr; 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 .AddHttpClients(configuration) .AddSingleton() .AddSingleton(serviceProvider => serviceProvider.GetRequiredService()) .AddServices() .AddHealthServices() .AddQuartzServices(configuration) .AddNotifications() .AddMassTransit(config => { config.DisableUsageTelemetry(); config.AddConsumer>(); config.AddConsumer>(); config.AddConsumer>(); config.AddConsumer>(); config.AddConsumer>(); config.AddConsumer>(); config.AddConsumer>(); config.AddConsumer>(); config.AddConsumer>(); config.UsingInMemory((context, cfg) => { cfg.ConfigureJsonSerializerOptions(options => { options.PropertyNameCaseInsensitive = true; 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.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(); // Add HTTP client for Plex authentication services.AddHttpClient("PlexAuth"); // Add HTTP client for OIDC authentication services.AddHttpClient("OidcAuth"); return services; } /// /// Adds health check services to the service collection /// private static IServiceCollection AddHealthServices(this IServiceCollection services) => services // Register the existing health check service for download clients .AddSingleton() // Register the background service for periodic health checks .AddHostedService() // Add ASP.NET Core health checks .AddHealthChecks() .AddCheck("application", tags: ["liveness"]) .AddCheck("database", tags: ["readiness"]) .AddCheck("filesystem", tags: ["readiness"]) .AddCheck("download_clients", tags: ["readiness"]) .Services; }