Publish Create/Update/Delete resource events to rabbit

This commit is contained in:
Zoe Roux 2024-03-17 18:32:36 +01:00
parent 3a5d6ed2cd
commit f1d72cb480
No known key found for this signature in database
7 changed files with 182 additions and 1 deletions

View File

@ -74,5 +74,6 @@ POSTGRES_PORT=5432
MEILI_HOST="http://meilisearch:7700"
MEILI_MASTER_KEY="ghvjkgisbgkbgskegblfqbgjkebbhgwkjfb"
RABBITMQ_HOST=rabbitmq:5672
RABBITMQ_DEFAULT_USER=kyoo
RABBITMQ_DEFAULT_PASS=aohohunuhouhuhhoahothonseuhaoensuthoaentsuhha

View File

@ -1,4 +1,5 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Microsoft Visual Studio Solution File, Format Version 12.00
#
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kyoo.Core", "src\Kyoo.Core\Kyoo.Core.csproj", "{0F8275B6-C7DD-42DF-A168-755C81B1C329}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kyoo.Abstractions", "src\Kyoo.Abstractions\Kyoo.Abstractions.csproj", "{BAB2CAE1-AC28-4509-AA3E-8DC75BD59220}"
@ -13,6 +14,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kyoo.Host", "src\Kyoo.Host\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kyoo.Meilisearch", "src\Kyoo.Meilisearch\Kyoo.Meilisearch.csproj", "{F8E6018A-FD51-40EB-99FF-A26BA59F2762}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{787FB205-9D7E-4946-AFE0-BD68E286F569}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kyoo.RabbitMq", "src\Kyoo.RabbitMq\Kyoo.RabbitMq.csproj", "{B97AD4A8-E6E6-41CD-87DF-5F1326FD7198}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -59,5 +64,18 @@ Global
{F8E6018A-FD51-40EB-99FF-A26BA59F2762}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F8E6018A-FD51-40EB-99FF-A26BA59F2762}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F8E6018A-FD51-40EB-99FF-A26BA59F2762}.Release|Any CPU.Build.0 = Release|Any CPU
{44F2208F-C015-4A01-8D6A-20F82437AFDB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{44F2208F-C015-4A01-8D6A-20F82437AFDB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44F2208F-C015-4A01-8D6A-20F82437AFDB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{44F2208F-C015-4A01-8D6A-20F82437AFDB}.Release|Any CPU.Build.0 = Release|Any CPU
{B97AD4A8-E6E6-41CD-87DF-5F1326FD7198}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B97AD4A8-E6E6-41CD-87DF-5F1326FD7198}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B97AD4A8-E6E6-41CD-87DF-5F1326FD7198}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B97AD4A8-E6E6-41CD-87DF-5F1326FD7198}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{0C8AA7EA-E723-4532-852F-35AA4E8AFED5} = {FEAE1B0E-D797-470F-9030-0EF743575ECC}
{44F2208F-C015-4A01-8D6A-20F82437AFDB} = {BB39454F-53E4-4238-9659-A39638496FB3}
{B97AD4A8-E6E6-41CD-87DF-5F1326FD7198} = {787FB205-9D7E-4946-AFE0-BD68E286F569}
EndGlobalSection
EndGlobal

View File

@ -26,6 +26,7 @@
<ProjectReference Include="../Kyoo.Core/Kyoo.Core.csproj" />
<ProjectReference Include="../Kyoo.Postgresql/Kyoo.Postgresql.csproj" />
<ProjectReference Include="../Kyoo.Meilisearch/Kyoo.Meilisearch.csproj" />
<ProjectReference Include="../Kyoo.RabbitMq/Kyoo.RabbitMq.csproj" />
<ProjectReference Include="../Kyoo.Authentication/Kyoo.Authentication.csproj" />
<ProjectReference Include="../Kyoo.Swagger/Kyoo.Swagger.csproj" />
</ItemGroup>

View File

@ -27,6 +27,7 @@ using Kyoo.Core;
using Kyoo.Host.Controllers;
using Kyoo.Meiliseach;
using Kyoo.Postgresql;
using Kyoo.RabbitMq;
using Kyoo.Swagger;
using Kyoo.Utils;
using Microsoft.AspNetCore.Builder;
@ -66,6 +67,7 @@ public class PluginsStartup
typeof(AuthenticationModule),
typeof(PostgresModule),
typeof(MeilisearchModule),
typeof(RabbitMqModule),
typeof(SwaggerModule)
);
}

View File

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Kyoo.RabbitMq</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Kyoo.Abstractions/Kyoo.Abstractions.csproj" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,53 @@
// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using Autofac;
using Kyoo.Abstractions.Controllers;
using Microsoft.Extensions.Configuration;
using RabbitMQ.Client;
namespace Kyoo.RabbitMq;
public class RabbitMqModule(IConfiguration configuration) : IPlugin
{
/// <inheritdoc />
public string Name => "RabbitMq";
/// <inheritdoc />
public void Configure(ContainerBuilder builder)
{
builder
.Register(
(_) =>
{
ConnectionFactory factory =
new()
{
UserName = configuration.GetValue("RABBITMQ_DEFAULT_USER", "guest"),
Password = configuration.GetValue("RABBITMQ_DEFAULT_USER", "guest"),
HostName = configuration.GetValue("RABBITMQ_HOST", "rabbitmq:5672"),
};
return factory.CreateConnection();
}
)
.AsSelf()
.SingleInstance();
builder.RegisterType<RabbitProducer>().AsSelf().SingleInstance().AutoActivate();
}
}

View File

@ -0,0 +1,90 @@
// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System.Text;
using System.Text.Json;
using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models;
using RabbitMQ.Client;
namespace Kyoo.RabbitMq;
public class RabbitProducer
{
private readonly IModel _channel;
public RabbitProducer(IConnection rabbitConnection)
{
_channel = rabbitConnection.CreateModel();
_channel.ExchangeDeclare(exchange: "events.resource.collection", type: ExchangeType.Fanout);
IRepository<Collection>.OnCreated += _Publish<Collection>("events.resource.collection", "created");
IRepository<Collection>.OnEdited += _Publish<Collection>("events.resource.collection", "edited");
IRepository<Collection>.OnDeleted += _Publish<Collection>("events.resource.collection", "deleted");
_channel.ExchangeDeclare(exchange: "events.resource.movie", type: ExchangeType.Fanout);
IRepository<Movie>.OnCreated += _Publish<Movie>("events.resource.movie", "created");
IRepository<Movie>.OnEdited += _Publish<Movie>("events.resource.movie", "edited");
IRepository<Movie>.OnDeleted += _Publish<Movie>("events.resource.movie", "deleted");
_channel.ExchangeDeclare(exchange: "events.resource.show", type: ExchangeType.Fanout);
IRepository<Show>.OnCreated += _Publish<Show>("events.resource.show", "created");
IRepository<Show>.OnEdited += _Publish<Show>("events.resource.show", "edited");
IRepository<Show>.OnDeleted += _Publish<Show>("events.resource.show", "deleted");
_channel.ExchangeDeclare(exchange: "events.resource.season", type: ExchangeType.Fanout);
IRepository<Season>.OnCreated += _Publish<Season>("events.resource.season", "created");
IRepository<Season>.OnEdited += _Publish<Season>("events.resource.season", "edited");
IRepository<Season>.OnDeleted += _Publish<Season>("events.resource.season", "deleted");
_channel.ExchangeDeclare(exchange: "events.resource.episode", type: ExchangeType.Fanout);
IRepository<Episode>.OnCreated += _Publish<Episode>("events.resource.episode", "created");
IRepository<Episode>.OnEdited += _Publish<Episode>("events.resource.episode", "edited");
IRepository<Episode>.OnDeleted += _Publish<Episode>("events.resource.episode", "deleted");
_channel.ExchangeDeclare(exchange: "events.resource.studio", type: ExchangeType.Fanout);
IRepository<Studio>.OnCreated += _Publish<Studio>("events.resource.studio", "created");
IRepository<Studio>.OnEdited += _Publish<Studio>("events.resource.studio", "edited");
IRepository<Studio>.OnDeleted += _Publish<Studio>("events.resource.studio", "deleted");
_channel.ExchangeDeclare(exchange: "events.resource.user", type: ExchangeType.Fanout);
IRepository<User>.OnCreated += _Publish<User>("events.resource.user", "created");
IRepository<User>.OnEdited += _Publish<User>("events.resource.user", "edited");
IRepository<User>.OnDeleted += _Publish<User>("events.resource.user", "deleted");
}
private IRepository<T>.ResourceEventHandler _Publish<T>(string exchange, string action)
where T : IResource, IQuery
{
return (T resource) =>
{
var message = new
{
Action = action,
Type = typeof(T).Name.ToLowerInvariant(),
Value = resource,
};
_channel.BasicPublish(
exchange,
routingKey: string.Empty,
body: Encoding.UTF8.GetBytes(JsonSerializer.Serialize(message))
);
return Task.CompletedTask;
};
}
}