Перепиливание под чистый DDD
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
using Knot.Shared.Kernel;
|
||||
using Knot.Shared.Kernel.Configuration;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MediatR;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Host.Application.WebRtc.Queries;
|
||||
|
||||
public record IceServerDto(string[] Urls, string? Username = null, string? Credential = null);
|
||||
public record IceServersResultDto(List<IceServerDto> IceServers);
|
||||
|
||||
public record GetIceServersQuery : IQuery<IceServersResultDto>;
|
||||
|
||||
internal sealed class GetIceServersQueryHandler : IQueryHandler<GetIceServersQuery, IceServersResultDto>
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ISettingsService _settingsService;
|
||||
|
||||
public GetIceServersQueryHandler(IConfiguration configuration, ISettingsService settingsService)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_settingsService = settingsService;
|
||||
}
|
||||
|
||||
public Task<Result<IceServersResultDto>> Handle(GetIceServersQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var settings = _settingsService.Current;
|
||||
if (!settings.EnableCalls)
|
||||
{
|
||||
return Task.FromResult(Result.Failure<IceServersResultDto>(new Error(
|
||||
Knot.Shared.Kernel.Constants.Errors.DisabledByAdmin,
|
||||
"СервРСвЂВВР РЋР С“ отключен Р°РТвЂВВР В Р’В Р РЋР’ВВР В Р’В Р РЋРІР‚ВВР Р…Р СвЂВВстратороРСВВВ."
|
||||
)));
|
||||
}
|
||||
|
||||
var turnUrl = !string.IsNullOrEmpty(settings.TurnHost)
|
||||
? $"turn:{settings.TurnHost}:{settings.TurnPort}"
|
||||
: _configuration["WebRtc:TurnUrl"];
|
||||
|
||||
var turnUsername = !string.IsNullOrEmpty(settings.TurnUser)
|
||||
? settings.TurnUser
|
||||
: _configuration["WebRtc:TurnUsername"];
|
||||
|
||||
var turnSecret = !string.IsNullOrEmpty(settings.TurnSecret)
|
||||
? settings.TurnSecret
|
||||
: _configuration["WebRtc:TurnPassword"];
|
||||
|
||||
var iceServers = new List<IceServerDto>();
|
||||
|
||||
if (!string.IsNullOrEmpty(turnUrl))
|
||||
{
|
||||
var stunUrl = turnUrl.Replace("turn:", "stun:");
|
||||
iceServers.Add(new IceServerDto(new[] { stunUrl }));
|
||||
|
||||
if (!string.IsNullOrEmpty(turnUsername))
|
||||
{
|
||||
iceServers.Add(new IceServerDto(
|
||||
new[] { turnUrl, turnUrl + "?transport=tcp" },
|
||||
turnUsername,
|
||||
!string.IsNullOrEmpty(turnSecret) ? turnSecret : turnUsername
|
||||
));
|
||||
}
|
||||
else
|
||||
{
|
||||
iceServers.Add(new IceServerDto(new[] { turnUrl, turnUrl + "?transport=tcp" }));
|
||||
}
|
||||
}
|
||||
|
||||
return Task.FromResult(Result.Success(new IceServersResultDto(iceServers)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Knot.Modules.WebRtc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
public static class DependencyInjection {
|
||||
public static IServiceCollection AddWebRtcModule(this IServiceCollection services) {
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Shared\Knot.Shared.Kernel\Knot.Shared.Kernel.csproj" />
|
||||
<ProjectReference Include="..\..\Shared\Knot.Shared.Infrastructure\Knot.Shared.Infrastructure.csproj" />
|
||||
<ProjectReference Include="..\Conversations\Knot.Modules.Conversations.csproj" />
|
||||
<ProjectReference Include="..\Auth\Knot.Modules.Auth.csproj" />
|
||||
<ProjectReference Include="..\Stories\Knot.Modules.Stories.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Carter" Version="10.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0-preview.1.25120.3" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,28 @@
|
||||
using Carter;
|
||||
using MediatR;
|
||||
using Knot.Shared.Kernel;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
|
||||
namespace Host.Endpoints;
|
||||
|
||||
/// <summary>
|
||||
/// РегРСвЂВВстрацРСвЂВВР РЋР РЏ РЎРЊР Р…Р ТвЂВВРїРѕРСвЂВВнтовWebRTC
|
||||
/// </summary>
|
||||
public sealed class WebRtcEndpoints : ICarterModule
|
||||
{
|
||||
public void AddRoutes(IEndpointRouteBuilder app)
|
||||
{
|
||||
var group = app.MapGroup(Knot.Shared.Kernel.Constants.Routes.ApiWebRtc)
|
||||
.RequireAuthorization();
|
||||
|
||||
group.MapGet("/ice-servers", async (ISender sender, CancellationToken ct) =>
|
||||
{
|
||||
var result = await sender.Send(new Host.Application.WebRtc.Queries.GetIceServersQuery(), ct);
|
||||
return result.IsSuccess ? Results.Ok(result.Value) : Results.BadRequest(result.Error);
|
||||
})
|
||||
.WithName("GetIceServers")
|
||||
.WithOpenApi();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user