2022-02-18 00:08:42 +00:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using NLog.Extensions.Logging;
|
|
|
|
|
using Selector.Cache.Extensions;
|
|
|
|
|
using Selector.CLI.Extensions;
|
|
|
|
|
using Selector.Events;
|
|
|
|
|
using Selector.Extensions;
|
|
|
|
|
using System;
|
|
|
|
|
using System.CommandLine;
|
|
|
|
|
using System.CommandLine.Invocation;
|
|
|
|
|
|
|
|
|
|
namespace Selector.CLI
|
|
|
|
|
{
|
|
|
|
|
public class HostRootCommand: RootCommand
|
|
|
|
|
{
|
|
|
|
|
public HostRootCommand()
|
|
|
|
|
{
|
|
|
|
|
Handler = CommandHandler.Create(() => HostCommand.Execute());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class HostCommand : Command
|
|
|
|
|
{
|
|
|
|
|
public HostCommand(string name, string description = null) : base(name, description)
|
|
|
|
|
{
|
|
|
|
|
Handler = CommandHandler.Create(() => Execute());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int Execute()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-06-28 07:30:27 +01:00
|
|
|
|
var host = CreateHostBuilder(Environment.GetCommandLineArgs(),ConfigureDefault, ConfigureDefaultNlog)
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
var logger = host.Services.GetRequiredService<ILogger<HostCommand>>();
|
|
|
|
|
var env = host.Services.GetRequiredService<IHostEnvironment>();
|
|
|
|
|
SetupExceptionHandling(logger, env);
|
|
|
|
|
|
|
|
|
|
host.Run();
|
2022-02-18 00:08:42 +00:00
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-28 07:30:27 +01:00
|
|
|
|
private static void SetupExceptionHandling(ILogger logger, IHostEnvironment env)
|
|
|
|
|
{
|
2023-01-06 21:11:29 +00:00
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += (_, e) =>
|
2022-06-28 07:30:27 +01:00
|
|
|
|
{
|
|
|
|
|
if(e.ExceptionObject is Exception ex)
|
|
|
|
|
{
|
2023-01-06 21:11:29 +00:00
|
|
|
|
logger.LogError(ex, "Unhandled exception thrown");
|
2022-06-28 07:30:27 +01:00
|
|
|
|
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-18 00:08:42 +00:00
|
|
|
|
public static RootOptions ConfigureOptions(HostBuilderContext context, IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.Configure<RootOptions>(options =>
|
|
|
|
|
{
|
|
|
|
|
OptionsHelper.ConfigureOptions(options, context.Configuration);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var config = OptionsHelper.ConfigureOptions(context.Configuration);
|
|
|
|
|
|
|
|
|
|
services.Configure<SpotifyAppCredentials>(options =>
|
|
|
|
|
{
|
|
|
|
|
options.ClientId = config.ClientId;
|
|
|
|
|
options.ClientSecret = config.ClientSecret;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ConfigureDefault(HostBuilderContext context, IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("~~~ Selector CLI ~~~");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("> Configuring...");
|
|
|
|
|
// CONFIG
|
|
|
|
|
var config = ConfigureOptions(context, services);
|
2022-02-26 21:28:51 +00:00
|
|
|
|
context.Configuration.ConfigureOptionsInjection(services);
|
2022-02-18 00:08:42 +00:00
|
|
|
|
|
|
|
|
|
Console.WriteLine("> Adding Services...");
|
|
|
|
|
// SERVICES
|
2022-02-18 23:10:58 +00:00
|
|
|
|
services.AddHttpClient()
|
|
|
|
|
.ConfigureDb(config);
|
|
|
|
|
|
2022-02-18 00:08:42 +00:00
|
|
|
|
services.AddConsumerFactories();
|
2022-09-30 08:41:44 +01:00
|
|
|
|
services.AddCLIConsumerFactories();
|
2022-02-18 00:08:42 +00:00
|
|
|
|
if (config.RedisOptions.Enabled)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> Adding caching consumers...");
|
|
|
|
|
services.AddCachingConsumerFactories();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
services.AddWatcher()
|
|
|
|
|
.AddEvents()
|
|
|
|
|
.AddSpotify();
|
|
|
|
|
|
|
|
|
|
services.ConfigureLastFm(config)
|
2022-02-25 21:07:22 +00:00
|
|
|
|
.ConfigureEqual(config)
|
|
|
|
|
.ConfigureJobs(config);
|
2022-02-18 00:08:42 +00:00
|
|
|
|
|
|
|
|
|
if (config.RedisOptions.Enabled)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> Adding Redis...");
|
|
|
|
|
services.AddRedisServices(config.RedisOptions.ConnectionString);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("> Adding cache event maps...");
|
|
|
|
|
services.AddTransient<IEventMapping, FromPubSub.SpotifyLink>()
|
|
|
|
|
.AddTransient<IEventMapping, FromPubSub.Lastfm>();
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("> Adding caching Spotify consumers...");
|
|
|
|
|
services.AddCachingSpotify();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HOSTED SERVICES
|
|
|
|
|
if (config.WatcherOptions.Enabled)
|
|
|
|
|
{
|
|
|
|
|
if (config.WatcherOptions.LocalEnabled)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> Adding Local Watcher Service");
|
|
|
|
|
services.AddHostedService<LocalWatcherService>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (config.DatabaseOptions.Enabled)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> Adding Db Watcher Service");
|
|
|
|
|
services.AddHostedService<DbWatcherService>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ConfigureDefaultNlog(HostBuilderContext context, ILoggingBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
builder.ClearProviders()
|
|
|
|
|
.SetMinimumLevel(LogLevel.Trace)
|
|
|
|
|
.AddNLog(context.Configuration);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 21:11:29 +00:00
|
|
|
|
static IHostBuilder CreateHostBuilder(string[] args, Action<HostBuilderContext, IServiceCollection> buildServices, Action<HostBuilderContext, ILoggingBuilder> buildLogs)
|
2022-02-18 00:08:42 +00:00
|
|
|
|
=> Host.CreateDefaultBuilder(args)
|
|
|
|
|
.UseSystemd()
|
2023-01-06 21:11:29 +00:00
|
|
|
|
.ConfigureServices((context, services) => buildServices(context, services))
|
|
|
|
|
.ConfigureLogging((context, builder) => buildLogs(context, builder));
|
2022-02-18 00:08:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|