using Microsoft.EntityFrameworkCore;
using SameBeat.Api.Hubs;
using SameBeat.Application.Interfejsy;
using SameBeat.Application.Serwisy;
using SameBeat.Infrastructure.Audio;
using SameBeat.Infrastructure.Autoryzacja;
using SameBeat.Infrastructure.BazaDanych;
using SameBeat.Infrastructure.LiveKit;
using SameBeat.Infrastructure.Repozytoria;
namespace SameBeat.Api.Konfiguracja;
/// Rejestracja DI dla warstw Application i Infrastructure -- zeby Program.cs zostal czytelny.
public static class RejestracjaUslug
{
public static IServiceCollection DodajUslugiSameBeat(this IServiceCollection services, IConfiguration konfiguracja)
{
services.AddDbContext(opcje =>
opcje.UseNpgsql(konfiguracja.GetConnectionString("BazaDanych")));
services.Configure(konfiguracja.GetSection("Jwt"));
services.Configure(konfiguracja.GetSection("LiveKit"));
services.Configure(konfiguracja.GetSection("Cors"));
services.Configure(konfiguracja.GetSection("BlokadaLogowania"));
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
// Klient administracyjny LiveKit Room Service (ETAP 5) -- typowany HttpClient, IHttpClientFactory
// zarzadza pulÄ… HttpMessageHandlerow (unika wyczerpania socketow przy krotkotrwalych klientach).
services.AddHttpClient();
// Adapter na SignalR dla SerwisTimeraMikrofonu (Infrastructure, dziala w tle poza requestem) --
// patrz komentarz w IRozgloszenieZdarzenImprezy. Bezstanowy, wiec singleton.
services.AddSingleton();
// ETAP 7 -- blokada konta po zbyt wielu nieudanych probach logowania. Ustawienia wyciagniete
// z IOptions tutaj (nie w konstruktorze SerwisLogowania), tak samo jak dla SerwisSceny/SerwisMikrofonu
// ponizej -- Application nie zna typow konfiguracyjnych z Infrastructure.
services.AddScoped(dostawca =>
{
var ustawieniaBlokady = dostawca.GetRequiredService>().Value;
return new SerwisLogowania(
dostawca.GetRequiredService(),
dostawca.GetRequiredService(),
dostawca.GetRequiredService(),
dostawca.GetRequiredService(),
ustawieniaBlokady.MaksymalnaLiczbaProb,
TimeSpan.FromSeconds(ustawieniaBlokady.CzasBlokadySekund),
dostawca.GetRequiredService>());
});
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
// SerwisDostepuLiveKit i SerwisPoczekalni potrzebuja samego Url LiveKit (bezpieczny do wyslania
// do frontendu) -- wyciagamy go z IOptions rejestracji, zeby Application nie znal typu
// UstawieniaLiveKit z Infrastructure.
services.AddScoped(dostawca =>
{
var ustawieniaLiveKit = dostawca.GetRequiredService>().Value;
return new SerwisDostepuLiveKit(
dostawca.GetRequiredService(),
ustawieniaLiveKit.Url,
ustawieniaLiveKit.NazwaPokojuGlowna,
dostawca.GetRequiredService>());
});
services.AddScoped(dostawca =>
{
var ustawieniaLiveKit = dostawca.GetRequiredService>().Value;
return new SerwisPoczekalni(
dostawca.GetRequiredService(),
dostawca.GetRequiredService(),
dostawca.GetRequiredService(),
dostawca.GetRequiredService(),
dostawca.GetRequiredService(),
ustawieniaLiveKit.Url,
ustawieniaLiveKit.NazwaPokojuPoczekalnia,
dostawca.GetRequiredService>());
});
// SerwisSceny wydaje token do pokoju GLOWNEGO (nie poczekalni!) -- to jest wlasnie miejsce
// przejscia uczestnika z poczekalni na scene widoczna dla widzow.
services.AddScoped(dostawca =>
{
var ustawieniaLiveKit = dostawca.GetRequiredService>().Value;
return new SerwisSceny(
dostawca.GetRequiredService(),
dostawca.GetRequiredService(),
dostawca.GetRequiredService(),
dostawca.GetRequiredService(),
dostawca.GetRequiredService(),
dostawca.GetRequiredService(),
dostawca.GetRequiredService(),
ustawieniaLiveKit.Url,
ustawieniaLiveKit.NazwaPokojuGlowna,
dostawca.GetRequiredService>());
});
// SerwisMikrofonu wola LiveKit dla pokoju GLOWNEGO -- mikrofon ma sens tylko dla uczestnika juz
// NaScenie (a wiec juz polaczonego z pokojem glownym, nie poczekalnia).
services.AddScoped(dostawca =>
{
var ustawieniaLiveKit = dostawca.GetRequiredService>().Value;
return new SerwisMikrofonu(
dostawca.GetRequiredService(),
dostawca.GetRequiredService(),
dostawca.GetRequiredService(),
dostawca.GetRequiredService(),
dostawca.GetRequiredService(),
dostawca.GetRequiredService(),
ustawieniaLiveKit.NazwaPokojuGlowna,
dostawca.GetRequiredService>());
});
// Autorytatywny timer mikrofonu (ETAP 5) -- patrz komentarz w SerwisTimeraMikrofonu. Musi byc
// zarejestrowany jako IHostedService, zeby ASP.NET Core wystartowal go razem z aplikacja.
services.AddHostedService();
return services;
}
}