Skip to content

Commit

Permalink
feat: Add a feature that allows you to optionally set whether to star…
Browse files Browse the repository at this point in the history
…t or not for all services.
  • Loading branch information
s2quake committed Aug 14, 2024
1 parent 1cc691a commit 55ea7ec
Show file tree
Hide file tree
Showing 23 changed files with 423 additions and 138 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Libplanet.Node.DependencyInjection;
using Libplanet.Node.Options.Schema;
using Microsoft.AspNetCore.Server.Kestrel.Core;

namespace Libplanet.Node.API.EntryPoints;

[Singleton<IApplicationEntryPoint>]
[Singleton<IApplicationBuilderEntryPoint>]
internal sealed class DefaultEntryPoint
: IApplicationEntryPoint, IApplicationBuilderEntryPoint
{
private const string HandlerMessage = """
Communication with gRPC endpoints must be made through a gRPC client. To learn how to
create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909
""";

public void Run(WebApplicationBuilder builder)
{
builder.Logging.AddConsole();

if (builder.Environment.IsDevelopment())
{
builder.WebHost.ConfigureKestrel(options =>
{
// Setup a HTTP/2 endpoint without TLS.
options.ListenLocalhost(5259, o => o.Protocols =
HttpProtocols.Http1AndHttp2);
});
}
}

public async Task RunAsync(WebApplication app, CancellationToken cancellationToken)
{
var serviceProvider = app.Services;
var schema = await OptionsSchemaBuilder.GetSchemaAsync(cancellationToken);
app.MapGet("/", () => HandlerMessage);
app.MapGet("/schema", () => schema);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Libplanet.Node.API.Extensions;
using Libplanet.Node.API.Options;
using Libplanet.Node.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Libplanet.Node.API.EntryPoints;

[Singleton<IApplicationBuilderEntryPoint>]
[Singleton<IApplicationEntryPoint>]
internal sealed class GrpcServiceEntryPoint(
IOptions<GrpcOptions> options, IWebHostEnvironment environment)
: IApplicationBuilderEntryPoint, IApplicationEntryPoint
{
private readonly GrpcOptions _options = options.Value;

public Task RunAsync(WebApplication app, CancellationToken cancellationToken)
{
var serviceProvider = app.Services;
var nodeBuilder = serviceProvider.GetRequiredService<INodeApplicationBuilder>();
app.MapGrpcServiceFromDomain(nodeBuilder.Scopes);
if (environment.IsDevelopment())
{
app.MapGrpcReflectionService().AllowAnonymous();
}

return Task.CompletedTask;
}

public void Run(WebApplicationBuilder builder)
{
if (_options.IsEnabled)
{
builder.Services.AddGrpc();

if (environment.IsDevelopment())
{
builder.Services.AddGrpcReflection();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
using System.Reflection;
using Libplanet.Node.DependencyInjection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;

namespace Libplanet.Node.Extensions;
namespace Libplanet.Node.API.Extensions;

public static class IEndpointRouteBuilderExtensions
internal static class IApplicationBuilderExtensions
{
public static IEndpointRouteBuilder MapGrpcServiceFromDomain(
this IEndpointRouteBuilder @this)
public static IApplicationBuilder MapGrpcServiceFromDomain(
this IApplicationBuilder @this)
=> @this.MapGrpcServiceFromDomain(scope: string.Empty);

public static IEndpointRouteBuilder MapGrpcServiceFromDomain(
this IEndpointRouteBuilder @this, string scope)
public static IApplicationBuilder MapGrpcServiceFromDomain(
this IApplicationBuilder @this, string scope)
{
var types = ServiceUtility.GetTypes(typeof(GrpcAttribute), inherit: true);
var methodType = typeof(GrpcEndpointRouteBuilderExtensions);
Expand All @@ -38,8 +36,8 @@ static IEnumerable<GrpcAttribute> GetAttributes(Type type, string scope)
return @this;
}

public static IEndpointRouteBuilder MapGrpcServiceFromDomain(
this IEndpointRouteBuilder @this, string[] scopes)
public static IApplicationBuilder MapGrpcServiceFromDomain(
this IApplicationBuilder @this, string[] scopes)
{
foreach (var scope in scopes)
{
Expand Down
1 change: 1 addition & 0 deletions sdk/node/Libplanet.Node.Executable/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Libplanet.Node;
14 changes: 14 additions & 0 deletions sdk/node/Libplanet.Node.Executable/Options/GrpcOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.ComponentModel;
using Libplanet.Node.DependencyInjection;
using Libplanet.Node.Options;

namespace Libplanet.Node.API.Options;

[Options(Position)]
public sealed class GrpcOptions : OptionsBase<GrpcOptions>
{
public const string Position = "Grpc";

[DefaultValue(true)]
public bool IsEnabled { get; set; }
}
48 changes: 1 addition & 47 deletions sdk/node/Libplanet.Node.Executable/Program.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,2 @@
using Libplanet.Node.Extensions;
using Libplanet.Node.Options.Schema;
using Microsoft.AspNetCore.Server.Kestrel.Core;

SynchronizationContext.SetSynchronizationContext(new());
var builder = WebApplication.CreateBuilder(args);

builder.Logging.AddConsole();

if (builder.Environment.IsDevelopment())
{
builder.WebHost.ConfigureKestrel(options =>
{
// Setup a HTTP/2 endpoint without TLS.
options.ListenLocalhost(5259, o => o.Protocols =
HttpProtocols.Http1AndHttp2);
});
}

// Additional configuration is required to successfully run gRPC on macOS.
// For instructions on how to configure Kestrel and gRPC clients on macOS,
// visit https://go.microsoft.com/fwlink/?linkid=2099682

// Add services to the container.
builder.Services.AddGrpc();
builder.Services.AddGrpcReflection();
var libplanetBuilder = builder.Services.AddLibplanetNode(builder.Configuration)
.WithSeed()
.WithNode();

var app = builder.Build();
var handlerMessage = """
Communication with gRPC endpoints must be made through a gRPC client. To learn how to
create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909
""";
var schema = await OptionsSchemaBuilder.GetSchemaAsync(default);

// Configure the HTTP request pipeline.
app.MapGrpcServiceFromDomain(libplanetBuilder.Scopes);
app.MapGet("/", () => handlerMessage);
app.MapGet("/schema", () => schema);

if (builder.Environment.IsDevelopment())
{
app.MapGrpcReflectionService().AllowAnonymous();
}

await using var app = new NodeApplication(args);
await app.RunAsync();
25 changes: 25 additions & 0 deletions sdk/node/Libplanet.Node.Executable/appsettings-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,17 @@
}
}
},
"Grpc": {
"title": "GrpcOptions",
"type": "object",
"additionalProperties": false,
"properties": {
"IsEnabled": {
"type": "boolean",
"default": true
}
}
},
"Genesis": {
"title": "GenesisOptions",
"type": "object",
Expand Down Expand Up @@ -1114,6 +1125,10 @@
"type": "object",
"additionalProperties": false,
"properties": {
"IsEnabled": {
"type": "boolean",
"default": true
},
"PrivateKey": {
"type": "string",
"description": "The private key of the node.",
Expand All @@ -1136,6 +1151,9 @@
"type": "object",
"additionalProperties": false,
"properties": {
"IsEnabled": {
"type": "boolean"
},
"PrivateKey": {
"type": "string",
"description": "The private key of the seed node.",
Expand Down Expand Up @@ -1174,6 +1192,9 @@
"type": "object",
"additionalProperties": false,
"properties": {
"IsEnabled": {
"type": "boolean"
},
"PrivateKey": {
"type": "string",
"description": "The private key of the seed node.",
Expand Down Expand Up @@ -1256,6 +1277,10 @@
{
"type": "object",
"properties": {
"Grpc": {
"description": "Type 'GrpcOptions' does not have a description.",
"$ref": "#/definitions/Grpc"
},
"Genesis": {
"description": "Options for the genesis block.",
"$ref": "#/definitions/Genesis"
Expand Down
13 changes: 13 additions & 0 deletions sdk/node/Libplanet.Node.Executable/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Kestrel": {
"EndpointDefaults": {
"Protocols": "Http1AndHttp2"
}
},
"Node": {
"IsEnabled": true
},
"BlocksyncSeed": {
"IsEnabled": true
},
"ConsensusSeed": {
"IsEnabled": true
},
"Grpc": {
"IsEnabled": true
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Microsoft.AspNetCore.Builder;

namespace Libplanet.Node.DependencyInjection;

public interface IApplicationBuilderEntryPoint
{
void Run(WebApplicationBuilder builder);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Microsoft.AspNetCore.Builder;

namespace Libplanet.Node.DependencyInjection;

public interface IApplicationEntryPoint
{
Task RunAsync(WebApplication app, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Libplanet.Node.Extensions.NodeBuilder;
using Libplanet.Node.Options;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -7,7 +6,7 @@ namespace Libplanet.Node.Extensions;

public static class LibplanetServicesExtensions
{
public static ILibplanetNodeBuilder AddLibplanetNode(
public static INodeApplicationBuilder AddLibplanetNode(
this IServiceCollection services,
IConfiguration configuration)
{
Expand All @@ -16,6 +15,9 @@ public static ILibplanetNodeBuilder AddLibplanetNode(
services.Configure<SoloProposeOption>(configuration.GetSection(SoloProposeOption.Position));
services.AddOptionsFromDomain(configuration);

return new LibplanetNodeBuilder(services, configuration);
var builder = new NodeApplicationBuilder(services, configuration);
services.AddSingleton<INodeApplicationBuilder>(builder);

return builder;
}
}
Loading

0 comments on commit 55ea7ec

Please sign in to comment.