2026-07-16 20:53:56 +00:00
|
|
|
using Heimdall.Application.Services;
|
|
|
|
|
using Heimdall.Domain.Entities;
|
|
|
|
|
using Heimdall.Infrastructure.Data;
|
|
|
|
|
using Heimdall.Infrastructure.Security;
|
|
|
|
|
using Heimdall.Infrastructure.Services;
|
2026-07-19 12:00:16 +00:00
|
|
|
using Heimdall.Infrastructure.Services.Discovery;
|
2026-07-16 20:53:56 +00:00
|
|
|
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,
|
2026-07-16 21:19:16 +00:00
|
|
|
string? environmentName = null)
|
2026-07-16 20:53:56 +00:00
|
|
|
{
|
2026-07-16 21:19:16 +00:00
|
|
|
var env = environmentName ?? Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
|
|
|
|
|
var isTesting = env == "Testing";
|
2026-07-16 20:53:56 +00:00
|
|
|
|
2026-07-17 13:15:25 +00:00
|
|
|
var connectionString = Environment.GetEnvironmentVariable("DATABASE_URL")
|
|
|
|
|
?? configuration.GetConnectionString("Default")
|
|
|
|
|
?? "Host=localhost;Port=5432;Database=heimdall;Username=heimdall;Password=heimdall";
|
2026-07-16 21:19:16 +00:00
|
|
|
|
2026-07-17 13:15:25 +00:00
|
|
|
if (isTesting)
|
|
|
|
|
{
|
|
|
|
|
var dbName = configuration.GetValue<string>("InMemoryDbName") ?? "HeimdallTestDb";
|
2026-07-16 20:53:56 +00:00
|
|
|
services.AddDbContext<AppDbContext>(options =>
|
2026-07-17 13:15:25 +00:00
|
|
|
options.UseInMemoryDatabase(dbName));
|
2026-07-18 20:01:31 +00:00
|
|
|
services.AddDbContextFactory<AppDbContext>(options =>
|
|
|
|
|
options.UseInMemoryDatabase(dbName));
|
2026-07-17 13:15:25 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
services.AddDbContext<AppDbContext>(options =>
|
2026-07-16 20:53:56 +00:00
|
|
|
options.UseNpgsql(connectionString, npgsql =>
|
2026-07-17 13:15:25 +00:00
|
|
|
npgsql.MigrationsHistoryTable("__EFMigrationsHistory", "public")));
|
2026-07-18 20:01:31 +00:00
|
|
|
services.AddDbContextFactory<AppDbContext>(options =>
|
|
|
|
|
options.UseNpgsql(connectionString, npgsql =>
|
|
|
|
|
npgsql.MigrationsHistoryTable("__EFMigrationsHistory", "public")));
|
2026-07-16 20:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
2026-07-17 20:07:03 +00:00
|
|
|
// Data Protection (for camera credential encryption)
|
|
|
|
|
services.AddDataProtection();
|
|
|
|
|
|
2026-07-16 20:53:56 +00:00
|
|
|
// JWT
|
|
|
|
|
services.AddScoped<IJwtService, JwtService>();
|
|
|
|
|
|
2026-07-17 20:07:03 +00:00
|
|
|
// Credential encryption
|
|
|
|
|
services.AddScoped<ICredentialProtector, DataProtectorCredentialProtector>();
|
|
|
|
|
|
2026-07-16 20:53:56 +00:00
|
|
|
// Application services
|
|
|
|
|
services.AddScoped<IAuthService, AuthService>();
|
|
|
|
|
services.AddScoped<ISiteService, SiteService>();
|
|
|
|
|
services.AddScoped<IAgentService, AgentService>();
|
2026-07-16 21:19:16 +00:00
|
|
|
services.AddScoped<IUserService, UserService>();
|
2026-07-17 06:18:56 +00:00
|
|
|
services.AddScoped<ICameraService, CameraService>();
|
2026-07-17 17:36:14 +00:00
|
|
|
services.AddScoped<IStatsService, StatsService>();
|
2026-07-17 18:14:41 +00:00
|
|
|
services.AddScoped<IAuditLogService, AuditLogService>();
|
2026-07-18 18:58:20 +00:00
|
|
|
services.AddScoped<ISettingService, SettingService>();
|
2026-07-19 06:49:17 +00:00
|
|
|
services.AddScoped<IGroupService, GroupService>();
|
2026-07-16 20:53:56 +00:00
|
|
|
|
2026-07-19 12:00:16 +00:00
|
|
|
// 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>();
|
2026-07-19 12:20:07 +00:00
|
|
|
services.AddTransient<DiscoveryCoordinatorService>();
|
|
|
|
|
|
|
|
|
|
// Assignment services
|
|
|
|
|
services.AddScoped<ICameraAssignmentService, CameraAssignmentService>();
|
2026-07-19 12:00:16 +00:00
|
|
|
|
2026-07-19 12:55:05 +00:00
|
|
|
// Event services
|
|
|
|
|
services.AddScoped<ICameraEventService, CameraEventService>();
|
2026-07-19 13:14:05 +00:00
|
|
|
services.AddSingleton<ISnapshotService, SnapshotService>();
|
|
|
|
|
|
|
|
|
|
// HTTP client for snapshot uploads (no default timeout)
|
|
|
|
|
services.AddHttpClient("Snapshot", c => c.Timeout = TimeSpan.FromMinutes(5));
|
2026-07-19 12:55:05 +00:00
|
|
|
|
2026-07-16 20:53:56 +00:00
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
}
|