using Amazon.Runtime; using Amazon.S3; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using mws.backend.dotnet.application.Auth; using mws.backend.dotnet.application.Common; using mws.backend.dotnet.application.Documents; using mws.backend.dotnet.application.Permissions; using mws.backend.dotnet.infrastructure.Authentication; using mws.backend.dotnet.infrastructure.Persistence; using mws.backend.dotnet.infrastructure.Storage; namespace mws.backend.dotnet.infrastructure; public static class DependencyInjection { public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) { var connectionString = configuration.GetConnectionString("Default") ?? "Host=localhost;Port=5432;Database=mws;Username=mws;Password=mws"; services.AddDbContext(options => options.UseNpgsql(connectionString)); services.AddScoped(); services.AddAutoMapper(cfg => { }, typeof(MappingProfile).Assembly); services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(IUnitOfWork).Assembly)); services.AddScoped(); var jwtSection = configuration.GetSection("Jwt"); var jwtOptions = jwtSection.Get() ?? new JwtOptions(); if (string.IsNullOrWhiteSpace(jwtOptions.Secret) || jwtOptions.Secret.Length < 32) { jwtOptions.Secret = Environment.GetEnvironmentVariable("MWS_JWT_SECRET") ?? "mws-development-secret-key-change-me-in-production-123456"; } services.Configure(jwtSection.Bind); services.AddSingleton(jwtOptions); services.AddScoped(); services.AddScoped(); services.Configure(configuration.GetSection("Minio")); services.AddSingleton(sp => { var options = sp.GetRequiredService>().Value; return new AmazonS3Client( new BasicAWSCredentials(options.AccessKey, options.SecretKey), new AmazonS3Config { ServiceURL = options.Endpoint, ForcePathStyle = true, UseHttp = options.Endpoint.StartsWith("http://", StringComparison.OrdinalIgnoreCase), RequestChecksumCalculation = RequestChecksumCalculation.WHEN_REQUIRED, ResponseChecksumValidation = ResponseChecksumValidation.WHEN_REQUIRED, }); }); services.AddScoped(); return services; } }