Skip to content

Commit

Permalink
feat: Add swagger and explorer(GQL)
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Aug 28, 2024
1 parent 895c277 commit 9ea7bf3
Show file tree
Hide file tree
Showing 21 changed files with 454 additions and 233 deletions.
396 changes: 198 additions & 198 deletions Libplanet.sln

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions sdk/node/Libplanet.Node.Executable/Explorer/BlockChainContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Reflection;
using Libplanet.Explorer.Indexing;
using Libplanet.Explorer.Interfaces;
using Libplanet.Net;
using Libplanet.Node.Services;
using Libplanet.Store;

namespace Libplanet.Node.API.Explorer;

internal sealed class BlockChainContext(
IBlockChainService blockChainService, ISwarmService swarmService) : IBlockChainContext
{
public bool Preloaded => false;

public Blockchain.BlockChain BlockChain => blockChainService.BlockChain;

#pragma warning disable S3011 // Reflection should not be used to increase accessibility ...
public IStore Store
{
get
{
var bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance;
var propertyInfo = typeof(BlockChain).GetProperty("Store", bindingFlags) ??
throw new InvalidOperationException("Store property not found.");
if (propertyInfo.GetValue(BlockChain) is IStore store)
{
return store;
}

throw new InvalidOperationException("Store property is not IStore.");
}
}
#pragma warning restore S3011

public Swarm Swarm => swarmService.Swarm;

public IBlockChainIndex Index => throw new NotSupportedException();
}
31 changes: 31 additions & 0 deletions sdk/node/Libplanet.Node.Executable/Explorer/ExplorerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Libplanet.Explorer;

namespace Libplanet.Node.API.Explorer;

public static class ExplorerExtensions
{
public static IServiceCollection AddNodeExplorer(
this IServiceCollection services)
{
var serviceProvider = services.BuildServiceProvider();

services.AddSingleton<BlockChainContext>();
services.AddSingleton<ExplorerStartup<BlockChainContext>>();
serviceProvider = services.BuildServiceProvider();
var startUp
= serviceProvider.GetRequiredService<ExplorerStartup<BlockChainContext>>();
startUp.ConfigureServices(services);

return services;
}

public static IApplicationBuilder UseNodeExplorer(this IApplicationBuilder builder)
{
var serviceProvider = builder.ApplicationServices;
var environment = serviceProvider.GetRequiredService<IWebHostEnvironment>();
var startUp = serviceProvider.GetService<ExplorerStartup<BlockChainContext>>();
startUp?.Configure(builder, environment);

return builder;
}
}
13 changes: 13 additions & 0 deletions sdk/node/Libplanet.Node.Executable/Explorer/ExplorerOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.ComponentModel;
using Libplanet.Node.Options;

namespace Libplanet.Node.API.Explorer;

[Options(Position)]
public sealed class ExplorerOptions : OptionsBase<ExplorerOptions>, IEnabledOptions
{
public const string Position = "Explorer";

[DefaultValue(true)]
public bool IsEnabled { get; set; } = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.40.0"/>
<PackageReference Include="Grpc.AspNetCore.Server.Reflection" Version="2.64.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.7.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.7.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.7.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.7" />
</ItemGroup>

<ItemGroup>
Expand All @@ -21,6 +25,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\tools\Libplanet.Explorer.Executable\Libplanet.Explorer.Executable.csproj" />
<ProjectReference Include="..\Libplanet.Node.Extensions\Libplanet.Node.Extensions.csproj" />
<ProjectReference Include="..\Libplanet.Node\Libplanet.Node.csproj" />
</ItemGroup>
Expand Down
47 changes: 30 additions & 17 deletions sdk/node/Libplanet.Node.Executable/Program.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,61 @@
using Libplanet.Node.API.Explorer;
using Libplanet.Node.API.Services;
using Libplanet.Node.API.Swagger;
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);
options.ListenLocalhost(5260, o => o.Protocols =
HttpProtocols.Http2);
});
}

// 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);
builder.Services.AddLibplanetNode(builder.Configuration);
if (builder.Services.IsOptionsEnabled<ExplorerOptions>())
{
builder.Services.AddNodeExplorer();
}

if (builder.Services.IsOptionsEnabled<SwaggerOptions>())
{
builder.Services.AddNodeSwagger();
}

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);
using var app = builder.Build();

// Configure the HTTP request pipeline.
app.MapGrpcService<BlockChainGrpcService>();
app.MapGrpcService<SchemaGrpcService>();
app.MapGrpcService<BlockChainGrpcServiceV1>();
app.MapGrpcService<SchemaGrpcServiceV1>();
app.MapGet("/", () => handlerMessage);
app.MapGet("/schema", () => schema);

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

app.MapSchemaBuilder("/v1/schema");
app.MapGet("/schema", context => Task.Run(() => context.Response.Redirect("/v1/schema")));

if (app.IsOptionsEnabled<ExplorerOptions>())
{
app.UseNodeExplorer();
}

if (app.IsOptionsEnabled<SwaggerOptions>())
{
app.UseNodeSwagger();
}

await app.RunAsync();
2 changes: 1 addition & 1 deletion sdk/node/Libplanet.Node.Executable/Protos/blockchain.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ syntax = "proto3";

option csharp_namespace = "Libplanet.Node.API";

package node;
package node.blockchain.v1;

service BlockChain {
rpc GetGenesisBlock (GetGenesisBlockRequest) returns (GetGenesisBlockReply);
Expand Down
2 changes: 1 addition & 1 deletion sdk/node/Libplanet.Node.Executable/Protos/schema.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ syntax = "proto3";

option csharp_namespace = "Libplanet.Node.API";

package node;
package node.schema.v1;

service Schema {
rpc GetList(GetListRequest) returns (GetListReply);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

namespace Libplanet.Node.API.Services;

public class BlockChainGrpcService(
IReadChainService blockChain)
: BlockChain.BlockChainBase
public class BlockChainGrpcServiceV1(IReadChainService blockChain) : BlockChain.BlockChainBase
{
private readonly IReadChainService _blockChain = blockChain;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Libplanet.Node.API.Services;

public class SchemaGrpcService : Schema.SchemaBase
internal sealed class SchemaGrpcServiceV1 : Schema.SchemaBase
{
private string[]? _list;
private string? _schema;
Expand Down
23 changes: 23 additions & 0 deletions sdk/node/Libplanet.Node.Executable/Swagger/SwaggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Libplanet.Node.API.Swagger;

internal static class SwaggerExtensions
{
public static IServiceCollection AddNodeSwagger(this IServiceCollection services)
{
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();

services.AddAuthorization();
services.AddAuthentication("Bearer").AddJwtBearer();

return services;
}

public static IApplicationBuilder UseNodeSwagger(this IApplicationBuilder builder)
{
builder.UseSwagger();
builder.UseSwaggerUI();

return builder;
}
}
13 changes: 13 additions & 0 deletions sdk/node/Libplanet.Node.Executable/Swagger/SwaggerOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.ComponentModel;
using Libplanet.Node.Options;

namespace Libplanet.Node.API.Swagger;

[Options(Position)]
public sealed class SwaggerOptions : OptionsBase<SwaggerOptions>, IEnabledOptions
{
public const string Position = "Swagger";

[DefaultValue(true)]
public bool IsEnabled { get; set; } = true;
}
30 changes: 30 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,28 @@
}
}
},
"Swagger": {
"title": "SwaggerOptions",
"type": "object",
"additionalProperties": false,
"properties": {
"IsEnabled": {
"type": "boolean",
"default": true
}
}
},
"Explorer": {
"title": "ExplorerOptions",
"type": "object",
"additionalProperties": false,
"properties": {
"IsEnabled": {
"type": "boolean",
"default": true
}
}
},
"Genesis": {
"title": "GenesisOptions",
"type": "object",
Expand Down Expand Up @@ -1221,6 +1243,14 @@
{
"type": "object",
"properties": {
"Swagger": {
"description": "Type 'SwaggerOptions' does not have a description.",
"$ref": "#/definitions/Swagger"
},
"Explorer": {
"description": "Type 'ExplorerOptions' does not have a description.",
"$ref": "#/definitions/Explorer"
},
"Genesis": {
"description": "Options for the genesis block.",
"$ref": "#/definitions/Genesis"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
},
"Kestrel": {
"EndpointDefaults": {
"Protocols": "Http1AndHttp2"
"Protocols": "Http2"
}
},
"Swarm": {
"IsEnabled": true
},
"Validator": {
"IsEnabled": true
},
"Explorer": {
"IsEnabled": true
},
"Swagger": {
"IsEnabled": true
}
}
Loading

0 comments on commit 9ea7bf3

Please sign in to comment.