Add DI support to CliFx

This commit is contained in:
2021-06-04 20:41:04 +02:00
parent de2cfdb632
commit 6b6325eeaf
2 changed files with 54 additions and 13 deletions

View File

@@ -1,3 +1,4 @@
using Akari.Prototype.Server.Cli.Commands;
using Akari.Prototype.Server.Options;
using CliFx;
using Microsoft.AspNetCore.Hosting;
@@ -16,16 +17,43 @@ namespace Akari.Prototype.Server
{
public class Program
{
public static async Task<int> Main() =>
await new CliApplicationBuilder()
public static async Task<int> Main(string[] args)
{
if (args?.Length > 0)
{
return await CliMain();
}
await AspMain(null);
return 0;
}
private static async Task<int> CliMain()
{
var host = CreateCliHostBuilder(null).Build();
CheckHostOptions(host);
return await new CliApplicationBuilder()
.AddCommandsFromThisAssembly()
.UseTypeActivator(host.Services.GetService)
.Build()
.RunAsync();
}
public static async Task AspMain(string[] args)
{
var host = CreateHostBuilder(args).Build();
var host = CreateWebHostBuilder(args).Build();
CheckHostOptions(host);
await host.RunAsync();
}
private static void CheckHostOptions(IHost host)
{
var services = host.Services;
var akariOptions = services.GetRequiredService<IOptions<AkariOptions>>().Value;
@@ -33,13 +61,21 @@ namespace Akari.Prototype.Server
{
Directory.CreateDirectory(akariOptions.DataPath);
}
await host.RunAsync();
}
// 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
public static IHostBuilder CreateHostBuilder(string[] args) =>
public static IHostBuilder CreateCliHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureHostConfiguration(configuration =>
{
configuration.AddJsonFile(TcpProviderOptions.FilePath, true);
configuration.AddJsonFile(AkariOptions.FilePath, false);
})
.ConfigureServices((hostContext, services) =>
{
Startup.ConfigureStandardServices(hostContext.Configuration, services);
});
public static IHostBuilder CreateWebHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureHostConfiguration(configuration =>
{

View File

@@ -28,21 +28,26 @@ namespace Akari.Prototype.Server
{
services.AddGrpc();
ConfigureStandardServices(Configuration, services);
services.AddHostedService<TcpProviderService>();
services.AddHostedService<AuthLifetimeService>();
}
public static void ConfigureStandardServices(IConfiguration configuration, IServiceCollection services)
{
services.AddOptions<TcpProviderOptions>()
.Bind(Configuration.GetSection(TcpProviderOptions.SectionPath))
.Bind(configuration.GetSection(TcpProviderOptions.SectionPath))
.ValidateDataAnnotations();
services.AddOptions<AkariOptions>()
.Bind(Configuration.GetSection(AkariOptions.SectionPath))
.Bind(configuration.GetSection(AkariOptions.SectionPath))
.ValidateDataAnnotations();
services.AddSingleton<IFingerprintManager, FingerprintManager>();
services.AddSingleton<IAuthManager, AuthManager>();
services.AddSingleton<IKeyManager, KeyManager>();
services.AddSingleton<AkariPath>();
services.AddHostedService<TcpProviderService>();
services.AddHostedService<AuthLifetimeService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.