options helper, optional db connection from CLI

This commit is contained in:
andy 2021-10-24 17:38:45 +01:00
parent 143f708cff
commit af980b130a
4 changed files with 58 additions and 15 deletions

View File

@ -1,7 +1,26 @@
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
namespace Selector.CLI namespace Selector.CLI
{ {
static class OptionsHelper {
public static void ConfigureOptions(RootOptions options, IConfiguration config)
{
config.GetSection(RootOptions.Key).Bind(options);
config.GetSection(FormatKeys( new[] { RootOptions.Key, WatcherOptions.Key})).Bind(options.WatcherOptions);
config.GetSection(FormatKeys( new[] { RootOptions.Key, DatabaseOptions.Key})).Bind(options.DatabaseOptions);
}
public static RootOptions ConfigureOptions(IConfiguration config)
{
var options = config.GetSection(RootOptions.Key).Get<RootOptions>();
ConfigureOptions(options, config);
return options;
}
public static string FormatKeys(string[] args) => string.Join(":", args);
}
class RootOptions class RootOptions
{ {
public const string Key = "Selector"; public const string Key = "Selector";
@ -15,6 +34,7 @@ namespace Selector.CLI
/// </summary> /// </summary>
public string ClientSecret { get; set; } public string ClientSecret { get; set; }
public WatcherOptions WatcherOptions { get; set; } = new(); public WatcherOptions WatcherOptions { get; set; } = new();
public DatabaseOptions DatabaseOptions { get; set; } = new();
public EqualityChecker Equality { get; set; } = EqualityChecker.Uri; public EqualityChecker Equality { get; set; } = EqualityChecker.Uri;
} }
@ -56,4 +76,11 @@ namespace Selector.CLI
{ {
AudioFeatures AudioFeatures
} }
class DatabaseOptions {
public const string Key = "Database";
public bool Enabled { get; set; } = false;
public string ConnectionString { get; set; }
}
} }

View File

@ -4,8 +4,11 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
using NLog.Extensions.Logging; using NLog.Extensions.Logging;
using Selector.Model;
namespace Selector.CLI namespace Selector.CLI
{ {
class Program class Program
@ -18,18 +21,19 @@ namespace Selector.CLI
static IHostBuilder CreateHostBuilder(string[] args) static IHostBuilder CreateHostBuilder(string[] args)
=> Host.CreateDefaultBuilder(args) => Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) => { .ConfigureServices((context, services) =>
{
Console.WriteLine("~~~ Selector CLI ~~~"); Console.WriteLine("~~~ Selector CLI ~~~");
Console.WriteLine(""); Console.WriteLine();
Console.WriteLine("> Configuring..."); Console.WriteLine("> Configuring...");
// CONFIG // CONFIG
services.Configure<RootOptions>(options => { services.Configure<RootOptions>(options =>
context.Configuration.GetSection(RootOptions.Key).Bind(options); {
context.Configuration.GetSection($"{RootOptions.Key}:{WatcherOptions.Key}").Bind(options.WatcherOptions);
OptionsHelper.ConfigureOptions(options, context.Configuration);
}); });
var config = context.Configuration.GetSection(RootOptions.Key).Get<RootOptions>(); var config = OptionsHelper.ConfigureOptions(context.Configuration);
Console.WriteLine("> Adding Services..."); Console.WriteLine("> Adding Services...");
// SERVICES // SERVICES
@ -40,6 +44,14 @@ namespace Selector.CLI
//services.AddSingleton<IRefreshTokenFactoryProvider, RefreshTokenFactoryProvider>(); //services.AddSingleton<IRefreshTokenFactoryProvider, RefreshTokenFactoryProvider>();
services.AddSingleton<IRefreshTokenFactoryProvider, CachingRefreshTokenFactoryProvider>(); services.AddSingleton<IRefreshTokenFactoryProvider, CachingRefreshTokenFactoryProvider>();
if (config.DatabaseOptions.Enabled)
{
Console.WriteLine("> Adding Databse Context...");
services.AddDbContext<SelectorContext>(options =>
options.UseNpgsql(config.DatabaseOptions.ConnectionString)
);
}
switch (config.Equality) switch (config.Equality)
{ {
case EqualityChecker.Uri: case EqualityChecker.Uri:
@ -60,7 +72,8 @@ namespace Selector.CLI
} }
}) })
.ConfigureLogging((context, builder) => { .ConfigureLogging((context, builder) =>
{
builder.ClearProviders(); builder.ClearProviders();
builder.SetMinimumLevel(LogLevel.Trace); builder.SetMinimumLevel(LogLevel.Trace);
builder.AddNLog(context.Configuration); builder.AddNLog(context.Configuration);

View File

@ -12,6 +12,9 @@
"consumers": [ "audiofeatures" ] "consumers": [ "audiofeatures" ]
} }
] ]
},
"Database": {
"enabled": true
} }
}, },
"Logging": { "Logging": {

View File

@ -43,10 +43,10 @@ namespace Selector.Web
services.Configure<IdentityOptions>(options => services.Configure<IdentityOptions>(options =>
{ {
// Password settings. // Password settings.
//options.Password.RequireDigit = true; options.Password.RequireDigit = false;
//options.Password.RequireLowercase = true; options.Password.RequireLowercase = false;
//options.Password.RequireNonAlphanumeric = true; options.Password.RequireNonAlphanumeric = false;
//options.Password.RequireUppercase = true; options.Password.RequireUppercase = false;
options.Password.RequiredLength = 3; options.Password.RequiredLength = 3;
options.Password.RequiredUniqueChars = 1; options.Password.RequiredUniqueChars = 1;