Skip to content

Commit

Permalink
Added specific configuration for RequestManager in GRPC
Browse files Browse the repository at this point in the history
  • Loading branch information
ppossanzini committed Sep 27, 2024
1 parent 3e117c6 commit f1415e7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
45 changes: 36 additions & 9 deletions Arbitrer.GRPC/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@

namespace Arbitrer.GRPC.Extensions
{


public static class Extensions
{

public const string ArbitrerGrpcCorsDefaultPolicy = "ArbitrerGRPCDefault";

public static void AddArbitrerGrpcCors(this CorsOptions corsOptions)
Expand Down Expand Up @@ -81,7 +78,31 @@ where typeof(IBaseRequest).IsAssignableFrom(t)

return options;
}


public static RequestsManagerOptions AcceptMessageFrom(this RequestsManagerOptions options,
Func<IEnumerable<Assembly>> assemblySelect)
{
var types = (
from a in assemblySelect()
from t in a.GetTypes()
where typeof(IBaseRequest).IsAssignableFrom(t)
select t).AsEnumerable();

foreach (var t in types)
options.AcceptMessageTypes.Add(t);

return options;
}

public static RequestsManagerOptions AcceptMessageFrom(this RequestsManagerOptions options,
Func<IEnumerable<Type>> typesSelect)
{
foreach (var type in typesSelect().Where(t => typeof(IBaseRequest).IsAssignableFrom(t)))
options.AcceptMessageTypes.Add(type);

return options;
}

/// <summary>
/// Add the Arbitrer RabbitMQ message dispatcher to the service collection, allowing it to be resolved and used.
Expand All @@ -91,26 +112,32 @@ where typeof(IBaseRequest).IsAssignableFrom(t)
/// <param name="grpcOptions">Grpc service configuration options, if not specified you need to call AddGrpc()
/// registration method.</param>
/// <returns>The updated service collection.</returns>
public static IServiceCollection AddArbitrerGrpcDispatcher(this IServiceCollection services,
Action<MessageDispatcherOptions> config,
public static IServiceCollection AddArbitrerGrpcDispatcher(this IServiceCollection services,
Action<MessageDispatcherOptions> config,
Action<GrpcServiceOptions> grpcOptions = null)
{

if (grpcOptions != null)
{
services.Configure(grpcOptions);
services.AddGrpc();
}

services.AddCors(o => o.AddArbitrerGrpcCors());

services.Configure<MessageDispatcherOptions>(config);
services.AddSingleton<IExternalMessageDispatcher, MessageDispatcher>();

return services;
}



public static IServiceCollection AddGrpsRequestManager(this IServiceCollection services, Action<RequestsManagerOptions> options)
{
if (options != null)
services.Configure(options);

return services;
}

public static IEndpointRouteBuilder UseGrpcRequestManager(this IEndpointRouteBuilder host)
{
host.MapGrpcService<RequestsManager>();
Expand Down
13 changes: 9 additions & 4 deletions Arbitrer.GRPC/RequestsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace Arbitrer.GRPC
{
public class RequestsManager : global::Arbitrer.GRPC.GrpcServices.GrpcServicesBase
{
private readonly ArbitrerOptions _arbitrerOptions;
private readonly ILogger<RequestsManager> _logger;
private readonly IServiceProvider _provider;
private readonly MessageDispatcherOptions _options;
Expand All @@ -31,14 +30,20 @@ public class RequestsManager : global::Arbitrer.GRPC.GrpcServices.GrpcServicesBa
private readonly Dictionary<string, Type> _typeMappings;

public RequestsManager(IOptions<MessageDispatcherOptions> options, ILogger<RequestsManager> logger, IServiceProvider provider,
IOptions<ArbitrerOptions> arbitrerOptions)
IOptions<ArbitrerOptions> arbitrerOptions, IOptions<RequestsManagerOptions> requestsManagerOptions)
{
this._arbitrerOptions = arbitrerOptions.Value;

if (requestsManagerOptions.Value.AcceptMessageTypes.Count == 0)
{
foreach (var t in arbitrerOptions.Value.LocalRequests)
requestsManagerOptions.Value.AcceptMessageTypes.Add(t);
}

this._options = options.Value;
this._logger = logger ?? throw new ArgumentNullException(nameof(logger));
this._provider = provider;

_typeMappings = _arbitrerOptions.LocalRequests.ToDictionary(k => k.TypeQueueName(_arbitrerOptions), v => v);
_typeMappings = requestsManagerOptions.Value.AcceptMessageTypes.ToDictionary(k => k.TypeQueueName(arbitrerOptions.Value), v => v);
}


Expand Down
9 changes: 9 additions & 0 deletions Arbitrer.GRPC/RequestsManagerOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
using System.Collections.Generic;

namespace Arbitrer.GRPC;

public class RequestsManagerOptions
{
public HashSet<Type> AcceptMessageTypes { get; private set; } = new HashSet<Type>();
}

0 comments on commit f1415e7

Please sign in to comment.