Skip to content

Commit

Permalink
Merge pull request #141 from itsharppro/dev
Browse files Browse the repository at this point in the history
(#140) udpate paralax grpc
  • Loading branch information
SaintAngeLs authored Oct 30, 2024
2 parents ad46a53 + 9596b28 commit 8f5ccee
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/Paralax.gRPC/src/Paralax.gRPC/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,37 @@
using Paralax.gRPC.Protobuf;
using Paralax.gRPC.Protobuf.Utilities;
using Paralax.Health;
using Paralax.gRPC.Utils;
using Paralax.gRPC.Utils;
using System.Diagnostics;
using Microsoft.Extensions.Options;

namespace Paralax.gRPC.Extensions
namespace Paralax.gRPC
{
public static class GrpcExtensions
{
public static IParalaxBuilder AddGrpc(this IParalaxBuilder builder, Action<GrpcOptionsBuilder> configureOptions)
private const string GrpcOptionsSection = "GrpcOptions";

public static IParalaxBuilder AddGrpc(this IParalaxBuilder builder, IConfiguration configuration, Action<GrpcOptionsBuilder> configureOptions = null)
{
var optionsBuilder = new GrpcOptionsBuilder();
configureOptions?.Invoke(optionsBuilder);
var grpcOptions = optionsBuilder.Build();
builder.Services.Configure<GrpcOptions>(configuration.GetSection(GrpcOptionsSection));

builder.Services.AddSingleton(grpcOptions);
if (configureOptions != null)
{
var optionsBuilder = new GrpcOptionsBuilder();
configureOptions.Invoke(optionsBuilder);
var grpcOptionsBuilt = optionsBuilder.Build();
builder.Services.AddSingleton(grpcOptionsBuilt);
}

builder.Services.AddCommonProtobufServices();

builder.Services.AddSingleton<UptimeService>();
builder.Services.AddSingleton<CpuUsageService>();
builder.Services.AddSingleton<MemoryUsageService>();

var serviceProvider = builder.Services.BuildServiceProvider();
var grpcOptions = serviceProvider.GetRequiredService<IOptions<GrpcOptions>>().Value;

builder.Services.Configure<KestrelServerOptions>(options =>
{
options.ListenAnyIP(grpcOptions.Port, listenOptions =>
Expand All @@ -42,13 +52,19 @@ public static IParalaxBuilder AddGrpc(this IParalaxBuilder builder, Action<GrpcO
options.MaxSendMessageSize = grpcOptions.MaxSendMessageSize;
});

if (grpcOptions.EnableReflection)
{
builder.Services.AddGrpcReflection();
}

return builder;
}

public static IApplicationBuilder UseGrpc(this IApplicationBuilder app, Action<IEndpointRouteBuilder> configureEndpoints)
{
using var scope = app.ApplicationServices.CreateScope();
var grpcOptions = scope.ServiceProvider.GetRequiredService<GrpcOptions>();

var grpcOptions = scope.ServiceProvider.GetRequiredService<IOptions<GrpcOptions>>().Value;

app.UseRouting();

Expand All @@ -68,7 +84,7 @@ public static IApplicationBuilder UseGrpc(this IApplicationBuilder app, Action<I
public static HealthCheckResponse CreateHealthCheckResponse(IServiceProvider serviceProvider, IConfiguration configuration)
{
var grpcOptions = new GrpcOptions();
configuration.GetSection("GrpcOptions").Bind(grpcOptions);
configuration.GetSection(GrpcOptionsSection).Bind(grpcOptions);

var uptimeService = serviceProvider.GetRequiredService<UptimeService>();
var cpuUsageService = serviceProvider.GetRequiredService<CpuUsageService>();
Expand Down

0 comments on commit 8f5ccee

Please sign in to comment.