Skip to content

Commit

Permalink
feat(grpc): add grpc-web support on server side
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Mar 25, 2024
1 parent 9301468 commit 4522f7d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
<PackageVersion Include="Grpc.Net.ClientFactory" Version="2.60.0" />
<PackageVersion Include="Grpc.Net.Client.Web" Version="2.60.0" />
<PackageVersion Include="Grpc.AspNetCore" Version="2.60.0" />
<PackageVersion Include="Grpc.AspNetCore.Web" Version="2.60.0" />
<PackageVersion Include="Grpc.AspNetCore.Server.Reflection" Version="2.60.0" />
</ItemGroup>
<!--.NET 8 specific libraries-->
Expand Down
21 changes: 16 additions & 5 deletions src/Sitko.Core.Grpc.Server/BaseGrpcServerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ public abstract class BaseGrpcServerModule<TConfig> : BaseApplicationModule<TCon
{
private readonly List<Action<IEndpointRouteBuilder>> endpointRegistrations = new();

public virtual void RegisterService<TService>(string? requiredAuthorizarionSchemeName) where TService : class =>
public virtual void RegisterService<TService>(string? requiredAuthorizationSchemeName, bool enableGrpcWeb = false)
where TService : class =>
endpointRegistrations.Add(builder =>
{
var grpcBuidler = builder.MapGrpcService<TService>();
if (!string.IsNullOrEmpty(requiredAuthorizarionSchemeName))
var grpcEndpoint = builder.MapGrpcService<TService>();
if (enableGrpcWeb)
{
grpcBuidler.RequireAuthorization(new AuthorizeAttribute
grpcEndpoint = grpcEndpoint.EnableGrpcWeb();
}

if (!string.IsNullOrEmpty(requiredAuthorizationSchemeName))
{
grpcEndpoint.RequireAuthorization(new AuthorizeAttribute
{
AuthenticationSchemes = requiredAuthorizarionSchemeName
AuthenticationSchemes = requiredAuthorizationSchemeName,
});
}
});
Expand Down Expand Up @@ -55,6 +61,11 @@ public void ConfigureEndpoints(IApplicationContext applicationContext,
IApplicationBuilder appBuilder, IEndpointRouteBuilder endpoints)
{
var config = GetOptions(appBuilder.ApplicationServices);
if (config.EnableGrpcWeb)
{
appBuilder.UseGrpcWeb();
}

foreach (var endpointRegistration in endpointRegistrations)
{
endpointRegistration(endpoints);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ public override async Task ApplicationStarted(IApplicationContext applicationCon
}
}

public override void RegisterService<TService>(string? requiredAuthorizarionSchemeName)
public override void RegisterService<TService>(string? requiredAuthorizationSchemeName, bool enableGrpcWeb = false)
{
base.RegisterService<TService>(requiredAuthorizarionSchemeName);
base.RegisterService<TService>(requiredAuthorizationSchemeName, enableGrpcWeb);
serviceRegistrations.Add(registrar => registrar.RegisterAsync<TService>());
healthChecksRegistrations.Add(healthCheckBuilder =>
healthCheckBuilder.AddCheck<GrpcServiceHealthCheck<TService>>(
$"Grpc service {typeof(TService).BaseType?.DeclaringType?.Name}"));
}
}

14 changes: 11 additions & 3 deletions src/Sitko.Core.Grpc.Server/GrpcServerModuleOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace Sitko.Core.Grpc.Server;
public class GrpcServerModuleOptions : BaseModuleOptions
{
private readonly List<Action<IGrpcServerModule>> serviceRegistrations = new();
internal string? RequiredAuthorizarionSchemeName { get; private set; }
internal string? RequiredAuthorizationSchemeName { get; private set; }
internal bool EnableGrpcWeb { get; private set; }
public string? Host { get; set; }
public int? Port { get; set; }
[JsonIgnore] public Action<IWebHostBuilder>? ConfigureWebHostDefaults { get; set; }
Expand All @@ -29,13 +30,20 @@ public class GrpcServerModuleOptions : BaseModuleOptions

public GrpcServerModuleOptions RegisterService<TService>() where TService : class
{
serviceRegistrations.Add(module => module.RegisterService<TService>(RequiredAuthorizarionSchemeName));
serviceRegistrations.Add(module => module.RegisterService<TService>(RequiredAuthorizationSchemeName));
return this;
}

public GrpcServerModuleOptions RegisterServiceWithGrpcWeb<TService>() where TService : class
{
EnableGrpcWeb = true;
serviceRegistrations.Add(module => module.RegisterService<TService>(RequiredAuthorizationSchemeName, true));
return this;
}

public GrpcServerModuleOptions RequireSchemeAuthorization(string schemeName)
{
RequiredAuthorizarionSchemeName = schemeName;
RequiredAuthorizationSchemeName = schemeName;
return this;
}
}
2 changes: 1 addition & 1 deletion src/Sitko.Core.Grpc.Server/IGrpcServerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace Sitko.Core.Grpc.Server;

public interface IGrpcServerModule
{
void RegisterService<TService>(string? requiredAuthorizarionSchemeName) where TService : class;
void RegisterService<TService>(string? requiredAuthorizationSchemeName, bool enableGrpcWeb = false) where TService : class;
}

1 change: 1 addition & 0 deletions src/Sitko.Core.Grpc.Server/Sitko.Core.Grpc.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Grpc.AspNetCore"/>
<PackageReference Include="Grpc.AspNetCore.Web"/>
<PackageReference Include="Grpc.AspNetCore.Server.Reflection"/>
</ItemGroup>
</Project>

0 comments on commit 4522f7d

Please sign in to comment.