using Heimdall.Application.Services; using Heimdall.Domain.Entities; using Heimdall.Infrastructure.Data; using Heimdall.Infrastructure.Security; using Heimdall.Infrastructure.Services; using Heimdall.Infrastructure.Services.Discovery; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace Heimdall.Infrastructure; public static class DependencyInjection { public static IServiceCollection AddInfrastructure( this IServiceCollection services, IConfiguration configuration, string? environmentName = null) { var env = environmentName ?? Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"; var isTesting = env == "Testing"; var connectionString = Environment.GetEnvironmentVariable("DATABASE_URL") ?? configuration.GetConnectionString("Default") ?? "Host=localhost;Port=5432;Database=heimdall;Username=heimdall;Password=heimdall"; if (isTesting) { var dbName = configuration.GetValue("InMemoryDbName") ?? "HeimdallTestDb"; services.AddDbContext(options => options.UseInMemoryDatabase(dbName)); services.AddDbContextFactory(options => options.UseInMemoryDatabase(dbName)); } else { services.AddDbContext(options => options.UseNpgsql(connectionString, npgsql => npgsql.MigrationsHistoryTable("__EFMigrationsHistory", "public"))); services.AddDbContextFactory(options => options.UseNpgsql(connectionString, npgsql => npgsql.MigrationsHistoryTable("__EFMigrationsHistory", "public"))); } // Identity services.AddIdentity(options => { options.Password.RequiredLength = 12; options.Password.RequireDigit = true; options.Password.RequireLowercase = true; options.Password.RequireUppercase = false; options.Password.RequireNonAlphanumeric = false; options.Lockout.MaxFailedAccessAttempts = 5; options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(15); }) .AddEntityFrameworkStores() .AddDefaultTokenProviders(); // Data Protection (for camera credential encryption) services.AddDataProtection(); // JWT services.AddScoped(); // Credential encryption services.AddScoped(); // Application services services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); // HTTP client for discovery probes services.AddHttpClient("Discovery", client => { client.DefaultRequestHeaders.ConnectionClose = true; }); // Camera discovery services services.AddSingleton(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); // Assignment services services.AddScoped(); // Event services services.AddScoped(); services.AddScoped(); services.AddSingleton(); // HTTP client for snapshot uploads (no default timeout) services.AddHttpClient("Snapshot", c => c.Timeout = TimeSpan.FromMinutes(5)); return services; } }