forked from planetarium/libplanet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
310 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,62 @@ | ||
using Libplanet.Node.API.Services; | ||
using Libplanet.Node.Extensions; | ||
using Libplanet.Node.Options.Schema; | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Server.Kestrel.Core; | ||
|
||
SynchronizationContext.SetSynchronizationContext(new()); | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Logging.AddConsole(); | ||
|
||
if (builder.Environment.IsDevelopment()) | ||
var builder = WebHost.CreateDefaultBuilder(args); | ||
var assemblies = new string[] | ||
{ | ||
"Libplanet.Node.Swagger", | ||
"Libplanet.Node.Explorer", | ||
}; | ||
builder.UseSetting(WebHostDefaults.HostingStartupAssembliesKey, string.Join(';', assemblies)); | ||
builder.ConfigureLogging(logging => | ||
{ | ||
logging.AddConsole(); | ||
}); | ||
builder.ConfigureKestrel((context, options) => | ||
{ | ||
builder.WebHost.ConfigureKestrel(options => | ||
if (context.HostingEnvironment.IsDevelopment()) | ||
{ | ||
// 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); | ||
|
||
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.MapGrpcService<BlockChainGrpcService>(); | ||
app.MapGrpcService<SchemaGrpcService>(); | ||
app.MapGet("/", () => handlerMessage); | ||
app.MapGet("/schema", () => schema); | ||
|
||
if (builder.Environment.IsDevelopment()) | ||
} | ||
}); | ||
builder.ConfigureServices((context, services) => | ||
{ | ||
app.MapGrpcReflectionService().AllowAnonymous(); | ||
} | ||
services.AddGrpc(); | ||
services.AddGrpcReflection(); | ||
services.AddLibplanetNode(context.Configuration); | ||
}); | ||
builder.Configure((context, app) => | ||
{ | ||
app.UseRouting(); | ||
app.UseEndpoints(endPoint => | ||
{ | ||
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 | ||
"""; | ||
string? schema = null; | ||
endPoint.MapGet(string.Empty, async context => | ||
{ | ||
await context.Response.WriteAsync(handlerMessage); | ||
}); | ||
endPoint.MapGet("/schema", async context => | ||
{ | ||
schema ??= await OptionsSchemaBuilder.GetSchemaAsync(context.RequestAborted); | ||
await context.Response.WriteAsync(schema); | ||
}); | ||
endPoint.MapGrpcService<BlockChainGrpcService>(); | ||
endPoint.MapGrpcService<SchemaGrpcService>(); | ||
if (context.HostingEnvironment.IsDevelopment()) | ||
{ | ||
endPoint.MapGrpcReflectionService().AllowAnonymous(); | ||
} | ||
}); | ||
}); | ||
|
||
using var app = builder.Build(); | ||
await app.RunAsync(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Reflection; | ||
using Libplanet.Blockchain; | ||
using Libplanet.Explorer.Indexing; | ||
using Libplanet.Explorer.Interfaces; | ||
using Libplanet.Net; | ||
using Libplanet.Node.Services; | ||
using Libplanet.Store; | ||
|
||
namespace Libplanet.Node.Explorer; | ||
|
||
internal sealed class BlockChainContext( | ||
IBlockChainService blockChainService, ISwarmService swarmService) : IBlockChainContext | ||
{ | ||
public bool Preloaded => false; | ||
|
||
public 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(); | ||
} |
46 changes: 46 additions & 0 deletions
46
sdk/node/Libplanet.Node.Explorer/ExplorerHostingStartup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Libplanet.Explorer; | ||
using Libplanet.Node.Explorer; | ||
using Libplanet.Node.Explorer.Options; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
|
||
[assembly: HostingStartup(typeof(ExplorerHostingStartup))] | ||
|
||
namespace Libplanet.Node.Explorer; | ||
|
||
internal sealed class ExplorerHostingStartup : IHostingStartup, IStartupFilter | ||
{ | ||
private ExplorerStartup<BlockChainContext>? _startUp; | ||
|
||
public void Configure(IWebHostBuilder builder) | ||
{ | ||
builder.ConfigureServices((context, services) => | ||
{ | ||
services.AddOptions<ExplorerOptions>() | ||
.Bind(context.Configuration.GetSection(ExplorerOptions.Position)); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
var options = serviceProvider.GetRequiredService<IOptions<ExplorerOptions>>().Value; | ||
|
||
if (options.IsEnabled) | ||
{ | ||
_startUp = new ExplorerStartup<BlockChainContext>(context.Configuration); | ||
_startUp.ConfigureServices(services); | ||
services.AddSingleton<IStartupFilter>(this); | ||
} | ||
}); | ||
} | ||
|
||
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) | ||
{ | ||
return builder => | ||
{ | ||
var serviceProvider = builder.ApplicationServices; | ||
var environment = serviceProvider.GetRequiredService<IWebHostEnvironment>(); | ||
_startUp?.Configure(builder, environment); | ||
next(builder); | ||
}; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
sdk/node/Libplanet.Node.Explorer/Libplanet.Node.Explorer.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<ItemGroup> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Libplanet.Node\Libplanet.Node.csproj" /> | ||
<ProjectReference Include="..\..\..\tools\Libplanet.Explorer.Executable\Libplanet.Explorer.Executable.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
13 changes: 13 additions & 0 deletions
13
sdk/node/Libplanet.Node.Explorer/Options/ExplorerOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.ComponentModel; | ||
using Libplanet.Node.Options; | ||
|
||
namespace Libplanet.Node.Explorer.Options; | ||
|
||
[Options(Position)] | ||
public sealed class ExplorerOptions : OptionsBase<ExplorerOptions> | ||
{ | ||
public const string Position = "Explorer"; | ||
|
||
[DefaultValue(true)] | ||
public bool IsEnabled { get; set; } = true; | ||
} |
1 change: 0 additions & 1 deletion
1
sdk/node/Libplanet.Node.Extensions/LibplanetServicesExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
sdk/node/Libplanet.Node.Swagger/Libplanet.Node.Swagger.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<ItemGroup> | ||
<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> | ||
<ProjectReference Include="..\Libplanet.Node\Libplanet.Node.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.ComponentModel; | ||
using Libplanet.Node.Options; | ||
|
||
namespace Libplanet.Node.Swagger.Options; | ||
|
||
[Options(Position)] | ||
public sealed class SwaggerOptions : OptionsBase<SwaggerOptions> | ||
{ | ||
public const string Position = "Swagger"; | ||
|
||
[DefaultValue(true)] | ||
public bool IsEnabled { get; set; } = true; | ||
} |
Oops, something went wrong.