1
0

Séparation des services

This commit is contained in:
MarcEricMartel 2023-11-16 06:46:31 -05:00
parent dc8740e556
commit 2c68f0e8a5
8 changed files with 166 additions and 48 deletions

30
.dockerignore Normal file
View File

@ -0,0 +1,30 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
!**/.gitignore
!.git/HEAD
!.git/config
!.git/packed-refs
!.git/refs/heads/**

View File

@ -4,11 +4,15 @@
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>ffc728b4-a681-4404-8156-a09f59c957d3</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..</DockerfileContext>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Aspire.StackExchange.Redis.OutputCaching" Version="8.0.0-preview.1.23557.2" /> <PackageReference Include="Aspire.StackExchange.Redis.OutputCaching" Version="8.0.0-preview.1.23557.2" />
<PackageReference Include="Foundry.Extensions.Canvas" Version="1.1.6" /> <PackageReference Include="Foundry.Extensions.Canvas" Version="1.1.6" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="[1.19.6-Preview.1, 1.19.6]" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup> </ItemGroup>

View File

@ -5,7 +5,6 @@ using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop; using Microsoft.JSInterop;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Newtonsoft.Json; using Newtonsoft.Json;
using StackExchange.Redis;
namespace BlazorCanvas.Server.Components.Data; namespace BlazorCanvas.Server.Components.Data;
@ -14,15 +13,13 @@ namespace BlazorCanvas.Server.Components.Data;
public class CanvasService { public class CanvasService {
private Canvas2DContext? _currentCanvasContext; private Canvas2DContext? _currentCanvasContext;
private IJSRuntime _jsRuntime; private IJSRuntime _jsRuntime;
private IConnectionMultiplexer _cache; private IRedisService _redisService;
private ChannelMessageQueue _channel;
private CanvasCommand _lastCommand = new(); private CanvasCommand _lastCommand = new();
private bool _is_started = false; private bool _is_started = false;
public CanvasService(IJSRuntime jsRuntime, IConnectionMultiplexer cache) { public CanvasService(IJSRuntime jsRuntime, IRedisService redisService) {
_jsRuntime = jsRuntime; _jsRuntime = jsRuntime;
_cache = cache; _redisService = redisService;
_channel = _cache.GetSubscriber().Subscribe(RedisChannel.Literal("lol"));
} }
public string currentColor { get; set; } = "Black"; public string currentColor { get; set; } = "Black";
@ -31,12 +28,13 @@ public class CanvasService {
public ElementReference divCanvas { get; set; } public ElementReference divCanvas { get; set; }
public BECanvasComponent myCanvas { get; set; } = new(); public BECanvasComponent myCanvas { get; set; } = new();
public async void Consume() { /// <summary>
/// Version Pub/Sub
/// </summary>
public async void Subscribe() {
CancellationToken cToken = new(); CancellationToken cToken = new();
while (!cToken.IsCancellationRequested) { while (!cToken.IsCancellationRequested) {
var mess = await _channel.ReadAsync(cToken); var comm = await _redisService.Subscribe(cToken);
var comm = JsonConvert.DeserializeObject<CanvasCommand>(mess.Message);
if (comm is not null) if (comm is not null)
Draw(comm); Draw(comm);
} }
@ -60,7 +58,12 @@ public class CanvasService {
_currentCanvasContext = await myCanvas.CreateCanvas2DAsync(); _currentCanvasContext = await myCanvas.CreateCanvas2DAsync();
await _currentCanvasContext.ClearRectAsync(0, 0, 1920, 1080); await _currentCanvasContext.ClearRectAsync(0, 0, 1920, 1080);
} }
await _currentCanvasContext.SetFillStyleAsync(command.Color); try {
await _currentCanvasContext.SetFillStyleAsync(command.Color);
} catch (JSDisconnectedException e) {
Console.WriteLine(e.ToString());
return; // Welp.
}
await _currentCanvasContext.FillRectAsync(command.X, command.Y, command.PointSize, command.PointSize); await _currentCanvasContext.FillRectAsync(command.X, command.Y, command.PointSize, command.PointSize);
} }
@ -68,7 +71,7 @@ public class CanvasService {
double mouseX = 0, mouseY = 0; double mouseX = 0, mouseY = 0;
if (!_is_started) { if (!_is_started) {
Consume(); Subscribe();
_is_started = true; _is_started = true;
} }
@ -103,7 +106,7 @@ public class CanvasService {
if (command.Equals(_lastCommand)) if (command.Equals(_lastCommand))
return; // Pour pas spammer des commandes si c'est pas pertinent. return; // Pour pas spammer des commandes si c'est pas pertinent.
_cache.GetSubscriber().Publish(RedisChannel.Literal("lol"), JsonConvert.SerializeObject(command)); _redisService.Publish(command);
//Draw(command); // Local //Draw(command); // Local

View File

@ -0,0 +1,6 @@
namespace BlazorCanvas.Server.Components.Data;
public interface IRedisService {
public Task<CanvasCommand?> Subscribe(CancellationToken cToken);
public void Publish(CanvasCommand command);
}

View File

@ -0,0 +1,36 @@
using Newtonsoft.Json;
using StackExchange.Redis;
namespace BlazorCanvas.Server.Components.Data;
// https://developer.redis.com/develop/dotnet/streams/stream-basics/
public class RedisService: IRedisService {
private IConnectionMultiplexer _cache;
private ChannelMessageQueue _channel;
public RedisService(IConnectionMultiplexer cache) {
_cache = cache;
_channel = _cache.GetSubscriber().Subscribe(RedisChannel.Literal("lol"));
}
/// <summary>
/// Version Pub/Sub
/// </summary>
public async Task<CanvasCommand?> Subscribe(CancellationToken cToken) {
var mess = await _channel.ReadAsync(cToken);
var comm = JsonConvert.DeserializeObject<CanvasCommand>(mess.Message);
if (comm is not null)
return comm;
else return null;
}
/// <summary>
/// Version Pub/Sub
/// </summary>
/// <param name="command"></param>
public async void Publish(CanvasCommand command) {
CanvasCommand cm = new(command);
await _cache.GetSubscriber().PublishAsync(_channel.Channel, JsonConvert.SerializeObject(cm));
}
}

View File

@ -0,0 +1,26 @@
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["BlazorCanvas/BlazorCanvas.Server/BlazorCanvas.Server.csproj", "BlazorCanvas/BlazorCanvas.Server/"]
COPY ["BlazorCanvas/BlazorCanvas.ServiceDefaults/BlazorCanvas.ServiceDefaults.csproj", "BlazorCanvas/BlazorCanvas.ServiceDefaults/"]
RUN dotnet restore "./BlazorCanvas/BlazorCanvas.Server/./BlazorCanvas.Server.csproj"
COPY . .
WORKDIR "/src/BlazorCanvas/BlazorCanvas.Server"
RUN dotnet build "./BlazorCanvas.Server.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./BlazorCanvas.Server.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "BlazorCanvas.Server.dll"]

View File

@ -10,9 +10,12 @@ builder.AddServiceDefaults();
// Add services to the container. // Add services to the container.
builder.Services.AddRazorComponents() builder.Services.AddRazorComponents()
.AddInteractiveServerComponents(); .AddInteractiveServerComponents();
builder.AddRedis("cache"); builder.AddRedis("cache");
builder.Services.AddScoped<CanvasService>(); builder.Services.AddTransient<IRedisService, RedisService>();
builder.Services.AddTransient<CanvasService>();
var app = builder.Build(); var app = builder.Build();
@ -27,7 +30,6 @@ if (!app.Environment.IsDevelopment())
} }
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseStaticFiles(); app.UseStaticFiles();
app.UseAntiforgery(); app.UseAntiforgery();

View File

@ -1,38 +1,49 @@
{ {
"$schema": "http://json.schemastore.org/launchsettings.json", "profiles": {
"iisSettings": { "http": {
"windowsAuthentication": false, "commandName": "Project",
"anonymousAuthentication": true, "launchBrowser": true,
"iisExpress": { "environmentVariables": {
"applicationUrl": "http://localhost:47589", "ASPNETCORE_ENVIRONMENT": "Development"
"sslPort": 44363 },
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5276"
},
"https": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7257;http://localhost:5276"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
} }
}, },
"profiles": { "Container (Dockerfile)": {
"http": { "commandName": "Docker",
"commandName": "Project", "launchBrowser": true,
"dotnetRunMessages": true, "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"launchBrowser": true, "environmentVariables": {
"applicationUrl": "http://localhost:5276", "ASPNETCORE_HTTPS_PORTS": "8081",
"environmentVariables": { "ASPNETCORE_HTTP_PORTS": "8080"
"ASPNETCORE_ENVIRONMENT": "Development"
}
}, },
"https": { "publishAllPorts": true,
"commandName": "Project", "useSSL": true
"dotnetRunMessages": true, }
"launchBrowser": true, },
"applicationUrl": "https://localhost:7257;http://localhost:5276", "$schema": "http://json.schemastore.org/launchsettings.json",
"environmentVariables": { "iisSettings": {
"ASPNETCORE_ENVIRONMENT": "Development" "windowsAuthentication": false,
} "anonymousAuthentication": true,
}, "iisExpress": {
"IIS Express": { "applicationUrl": "http://localhost:47589",
"commandName": "IISExpress", "sslPort": 44363
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
} }
} }
}