Files
mws.backend.dotnet/infrastructure/DependencyInjection.cs
T

55 lines
2.5 KiB
C#
Raw Normal View History

2026-08-13 23:10:22 +07:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
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;
2026-09-06 12:33:38 +07:00
using mws.backend.dotnet.infrastructure.CloudFile;
2026-08-19 23:25:08 +07:00
using mws.backend.dotnet.infrastructure.Persistence;
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-08-13 23:12:57 +07:00
services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(IUnitOfWork).Assembly));
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-06 12:33:38 +07:00
services.Configure<CloudFileOptions>(configuration.GetSection("CloudFile"));
services.AddHttpClient<ICloudFileHttpClient, CloudFileHttpClient>((sp, http) =>
{
var options = sp.GetRequiredService<Microsoft.Extensions.Options.IOptions<CloudFileOptions>>().Value;
#pragma warning disable S1075 // hardcoded URL — this is a configurable base address from appsettings
http.BaseAddress = new Uri(options.BaseUrl.TrimEnd('/') + "/");
#pragma warning restore S1075
http.Timeout = TimeSpan.FromSeconds(options.HttpTimeoutSeconds);
});
services.AddScoped<IDocumentContentStore, CloudFileDocumentContentStore>();
2026-08-13 23:10:22 +07:00
return services;
}
}