Files

71 lines
3.0 KiB
C#
Raw Permalink Normal View History

2026-09-15 21:13:37 +07:00
using Amazon.Runtime;
using Amazon.S3;
2026-08-13 23:10:22 +07:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
2026-09-15 21:13:37 +07:00
using Microsoft.Extensions.Options;
2026-09-20 16:21:15 +07:00
using MediatR;
using mws.backend.dotnet.application.Audit;
2026-08-19 23:25:08 +07:00
using mws.backend.dotnet.application.Auth;
using mws.backend.dotnet.application.Common;
2026-09-06 12:33:38 +07:00
using mws.backend.dotnet.application.Documents;
2026-08-19 23:25:08 +07:00
using mws.backend.dotnet.application.Permissions;
using mws.backend.dotnet.infrastructure.Authentication;
using mws.backend.dotnet.infrastructure.Persistence;
2026-09-15 21:13:37 +07:00
using mws.backend.dotnet.infrastructure.Storage;
2026-08-13 23:10:22 +07:00
2026-08-19 23:25:08 +07:00
namespace mws.backend.dotnet.infrastructure;
2026-08-13 23:10:22 +07:00
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<AppDbContext>(options => options.UseNpgsql(connectionString));
services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddAutoMapper(cfg => { }, typeof(MappingProfile).Assembly);
2026-09-20 16:21:15 +07:00
services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssembly(typeof(IUnitOfWork).Assembly);
cfg.AddOpenBehavior(typeof(AuditLogBehavior<,>));
});
services.AddScoped<IAuditContext, AuditContext>();
2026-08-13 23:10:22 +07:00
services.AddScoped<IPasswordHasher, BcryptPasswordHasher>();
var jwtSection = configuration.GetSection("Jwt");
var jwtOptions = jwtSection.Get<JwtOptions>() ?? 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<JwtOptions>(jwtSection.Bind);
services.AddSingleton(jwtOptions);
services.AddScoped<ITokenService, JwtTokenService>();
services.AddScoped<IPermissionService, PermissionService>();
2026-09-15 21:13:37 +07:00
services.Configure<MinioOptions>(configuration.GetSection("Minio"));
services.AddSingleton<IAmazonS3>(sp =>
2026-09-06 12:33:38 +07:00
{
2026-09-15 21:13:37 +07:00
var options = sp.GetRequiredService<IOptions<MinioOptions>>().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,
});
2026-09-06 12:33:38 +07:00
});
2026-09-15 21:13:37 +07:00
services.AddScoped<IDocumentContentStore, S3DocumentContentStore>();
2026-09-06 12:33:38 +07:00
2026-08-13 23:10:22 +07:00
return services;
}
}