Files
Heimdall/src/Heimdall.Infrastructure/DependencyInjection.cs
T

108 lines
4.5 KiB
C#
Raw Normal View History

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<string>("InMemoryDbName") ?? "HeimdallTestDb";
services.AddDbContext<AppDbContext>(options =>
options.UseInMemoryDatabase(dbName));
services.AddDbContextFactory<AppDbContext>(options =>
options.UseInMemoryDatabase(dbName));
}
else
{
services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(connectionString, npgsql =>
npgsql.MigrationsHistoryTable("__EFMigrationsHistory", "public")));
services.AddDbContextFactory<AppDbContext>(options =>
options.UseNpgsql(connectionString, npgsql =>
npgsql.MigrationsHistoryTable("__EFMigrationsHistory", "public")));
}
// Identity
services.AddIdentity<AppUser, AppRole>(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<AppDbContext>()
.AddDefaultTokenProviders();
// Data Protection (for camera credential encryption)
services.AddDataProtection();
// JWT
services.AddScoped<IJwtService, JwtService>();
// Credential encryption
services.AddScoped<ICredentialProtector, DataProtectorCredentialProtector>();
// Application services
services.AddScoped<IAuthService, AuthService>();
services.AddScoped<ISiteService, SiteService>();
services.AddScoped<IAgentService, AgentService>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<ICameraService, CameraService>();
services.AddScoped<IStatsService, StatsService>();
services.AddScoped<IAuditLogService, AuditLogService>();
services.AddScoped<ISettingService, SettingService>();
services.AddScoped<IGroupService, GroupService>();
// HTTP client for discovery probes
services.AddHttpClient("Discovery", client =>
{
client.DefaultRequestHeaders.ConnectionClose = true;
});
// Camera discovery services
services.AddSingleton<StreamTesterService>();
services.AddTransient<IOnvifProbeService, OnvifProbeService>();
services.AddTransient<IMdnsProbeService, MdnsProbeService>();
services.AddTransient<IHttpProbeService, HttpProbeService>();
services.AddTransient<IArpProbeService, ArpProbeService>();
services.AddTransient<IPortScanService, PortScanService>();
services.AddTransient<ICameraDiscoveryService, CameraDiscoveryService>();
services.AddTransient<DiscoveryCoordinatorService>();
// Assignment services
services.AddScoped<ICameraAssignmentService, CameraAssignmentService>();
// Event services
services.AddScoped<ICameraEventService, CameraEventService>();
services.AddSingleton<ISnapshotService, SnapshotService>();
// HTTP client for snapshot uploads (no default timeout)
services.AddHttpClient("Snapshot", c => c.Timeout = TimeSpan.FromMinutes(5));
return services;
}
}