using System.Diagnostics.CodeAnalysis; using Microsoft.AspNetCore.Builder; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Http.Resilience; using Polly; using SlpModularCms.Core.Hosting; using SlpModularCms.Core.Modules; using SlpModularCms.Modules.Master.BackgroundServices; using SlpModularCms.Modules.Master.Data; using SlpModularCms.Modules.Master.Config; using SlpModularCms.Modules.Master.Repositories; using SlpModularCms.Modules.Master.Services; namespace SlpModularCms.Modules.Master; [ExcludeFromCodeCoverage] public class MasterModule : IModule { public string Name => "Master"; public string Version => "1.0.0"; public void RegisterServices(IServiceCollection services) { // Data Protection is configured once by the host (AddCmsDataProtection), NOT here. // See the equivalent note in AvailabilityModule: a bare AddDataProtection() call here // would override the host's persistent key store, and the resulting defect is invisible // until the first release switch makes every stored slave API key undecryptable. services.AddSingleton(); services.AddOptions().BindConfiguration("MasterModule"); services.AddDbContext((serviceProvider, options) => { var config = serviceProvider.GetRequiredService(); options.UseMySQL(config.GetConnectionString("DefaultConnection")!); options.ReplaceService(); }); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddHttpClient() .AddResilienceHandler("slave-resilience", (builder, context) => { var opts = context.ServiceProvider.GetRequiredService>(); builder.AddRetry(new HttpRetryStrategyOptions { MaxRetryAttempts = 2, Delay = TimeSpan.FromSeconds(1), BackoffType = DelayBackoffType.Exponential, UseJitter = true }); builder.AddTimeout(TimeSpan.FromSeconds(opts.Value.HttpTimeoutSeconds)); }); services.AddHostedService(); services.AddHttpContextAccessor(); } public void UseModule(IApplicationBuilder app) { using var scope = app.ApplicationServices.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); db.Database.Migrate(); } }