Skip to content

Commit

Permalink
Added GRPC over HTTP2
Browse files Browse the repository at this point in the history
  • Loading branch information
ppossanzini committed Sep 25, 2024
1 parent e88f319 commit a6504fa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions Arbitrer.GRPC/Arbitrer.GRPC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.25.2" />
<PackageReference Include="Grpc.AspNetCore.Web" Version="2.66.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.*"/>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
Expand Down
38 changes: 35 additions & 3 deletions Arbitrer.GRPC/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Security.Cryptography;
using System.Text;
using Arbitrer.GRPC;
using Grpc.AspNetCore.Server;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Routing;
Expand All @@ -10,29 +11,60 @@

namespace Arbitrer.GRPC.Extensions
{


public static class Extensions
{

const string ArbitrerGrpcCorsDefaultPolicy = "ArbitrerGRPCDefault";

/// <summary>
/// Add the Arbitrer RabbitMQ message dispatcher to the service collection, allowing it to be resolved and used.
/// </summary>
/// <param name="services">The service collection to add the message dispatcher to.</param>
/// <param name="config">The configuration settings for the message dispatcher.</param>
/// <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)
{
services.AddGrpc();

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

services.AddCors(o => o.AddPolicy(ArbitrerGrpcCorsDefaultPolicy, builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.WithExposedHeaders("Grpc-Status", "Grpc-Message", "Grpc-Encoding", "Grpc-Accept-Encoding");
}));

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

return services;
}

public static IEndpointRouteBuilder UseGrpcDispatcher(this IEndpointRouteBuilder host)
{
host.MapGrpcService<RequestsManager>();
return host;
}

public static IEndpointRouteBuilder UseGrpcWebDispatcher(this IEndpointRouteBuilder host)
{
host.MapGrpcService<RequestsManager>()
.EnableGrpcWeb()
.RequireCors(ArbitrerGrpcCorsDefaultPolicy);
return host;
}

public static string GetHash(this string input, HashAlgorithm hashAlgorithm)
{
byte[] data = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(input));
Expand Down
2 changes: 2 additions & 0 deletions arbitrer.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AGrpcEndpointRouteBuilderExtensions_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FSourcesCache_003F81fbefe85edaa9b9bcb98544a4383964cce3c5c7dba2ebbf3e6692a6bbdbc8_003FGrpcEndpointRouteBuilderExtensions_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AGrpcServiceExtensions_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FSourcesCache_003F8d32a395c82b7ab85776f2a87e765a339586765635c5b37d26a9b5e759e8_003FGrpcServiceExtensions_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>


<s:String x:Key="/Default/CodeInspection/PencilsConfiguration/ActualSeverity/@EntryValue">INFO</s:String>
Expand Down

0 comments on commit a6504fa

Please sign in to comment.