using Hutopy.Infrastructure.BlobStorage; using Hutopy.Infrastructure.Data; using Hutopy.Infrastructure.Identity; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace Hutopy.Infrastructure; public static class DependencyInjection { public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration) { // Replace password in the connection string with env var in local environment. // Prod will use the connectionString stored in the vault with password in it directly. var connectionString = configuration.GetConnectionString("MssqlConnection") ?? throw new InvalidOperationException("Missing ConnectionStrings:MssqlConnection"); services.AddDbContext((sp, options) => { options.AddInterceptors(sp.GetServices()); options.UseSqlServer(connectionString); }); services.AddScoped(); services.AddAuthentication() .AddBearerToken(IdentityConstants.BearerScheme); services.AddAuthorizationBuilder(); services .AddIdentityCore() .AddUserManager() .AddRoles() .AddEntityFrameworkStores() .AddApiEndpoints() .AddSignInManager>() .AddDefaultTokenProviders(); // Singleton services services.AddSingleton(TimeProvider.System); services.AddSingleton(); // Scoped services services.AddScoped(); return services; } }