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;
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
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-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();
|
|
|
|
|
|
|
|
|
|
// JWT
|
|
|
|
|
services.AddScoped<IJwtService, JwtService>();
|
|
|
|
|
|
|
|
|
|
// 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-16 20:53:56 +00:00
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
}
|