some changes
This commit is contained in:
parent
4ec9720541
commit
b47bac67ca
37 changed files with 397 additions and 190 deletions
|
@ -29,8 +29,4 @@
|
|||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Infrastructure\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
32
Femto.Api/Infrastructure/EventBus.cs
Normal file
32
Femto.Api/Infrastructure/EventBus.cs
Normal file
|
@ -0,0 +1,32 @@
|
|||
using System.Threading.Channels;
|
||||
using Femto.Common.Integration;
|
||||
|
||||
namespace Femto.Api.Infrastructure;
|
||||
|
||||
public class EventBus(Channel<IEvent> channel) : BackgroundService, IEventBus
|
||||
{
|
||||
private readonly ICollection<IEventBus.Subscriber> _subscribers = [];
|
||||
|
||||
public Task Publish<T>(T evt)
|
||||
where T : IEvent
|
||||
{
|
||||
channel.Writer.TryWrite(evt);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Subscribe(IEventBus.Subscriber subscriber)
|
||||
{
|
||||
this._subscribers.Add(subscriber);
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
await foreach (var message in channel.Reader.ReadAllAsync(stoppingToken))
|
||||
{
|
||||
await Task.WhenAll(
|
||||
this._subscribers.Select(subscriber => subscriber.Invoke(message, stoppingToken))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,9 +1,12 @@
|
|||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Channels;
|
||||
using Femto.Api;
|
||||
using Femto.Api.Auth;
|
||||
using Femto.Api.Infrastructure;
|
||||
using Femto.Common;
|
||||
using Femto.Common.Domain;
|
||||
using Femto.Common.Integration;
|
||||
using Femto.Modules.Auth.Application;
|
||||
using Femto.Modules.Blog.Application;
|
||||
using Femto.Modules.Media.Application;
|
||||
|
@ -27,9 +30,12 @@ if (blobStorageRoot is null)
|
|||
throw new Exception("no blob storage root found");
|
||||
|
||||
|
||||
builder.Services.InitializeBlogModule(connectionString);
|
||||
var eventBus = new EventBus(Channel.CreateUnbounded<IEvent>());
|
||||
builder.Services.AddHostedService(_ => eventBus);
|
||||
|
||||
builder.Services.InitializeBlogModule(connectionString, eventBus);
|
||||
builder.Services.InitializeMediaModule(connectionString, blobStorageRoot);
|
||||
builder.Services.InitializeAuthenticationModule(connectionString);
|
||||
builder.Services.InitializeAuthenticationModule(connectionString, eventBus);
|
||||
|
||||
builder.Services.AddScoped<CurrentUserContext, CurrentUserContext>();
|
||||
builder.Services.AddScoped<ICurrentUserContext>(s => s.GetRequiredService<CurrentUserContext>());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue