2021-10-08 23:48:07 +01:00
|
|
|
|
using System;
|
2021-10-11 01:06:43 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2021-11-03 17:38:54 +00:00
|
|
|
|
using System.CommandLine;
|
2021-10-08 23:48:07 +01:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2021-10-11 01:06:43 +01:00
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2021-10-08 23:48:07 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-10-24 17:38:45 +01:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-10-11 18:17:36 +01:00
|
|
|
|
using NLog.Extensions.Logging;
|
2021-10-08 23:48:07 +01:00
|
|
|
|
|
2021-11-29 21:48:00 +00:00
|
|
|
|
using Selector.Extensions;
|
2021-10-24 17:38:45 +01:00
|
|
|
|
using Selector.Model;
|
2021-10-27 23:00:01 +01:00
|
|
|
|
using Selector.Cache;
|
2021-11-29 21:04:15 +00:00
|
|
|
|
using Selector.Cache.Extensions;
|
2021-12-19 18:44:03 +00:00
|
|
|
|
using Selector.Events;
|
2021-10-24 17:38:45 +01:00
|
|
|
|
|
2021-10-08 23:48:07 +01:00
|
|
|
|
namespace Selector.CLI
|
|
|
|
|
{
|
2021-11-29 21:04:15 +00:00
|
|
|
|
public static class Program
|
2021-10-08 23:48:07 +01:00
|
|
|
|
{
|
2021-10-11 01:06:43 +01:00
|
|
|
|
public static async Task Main(string[] args)
|
2021-10-08 23:48:07 +01:00
|
|
|
|
{
|
2021-11-03 17:38:54 +00:00
|
|
|
|
var cmd = new RootCommand {
|
|
|
|
|
new Command("start") {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-12-15 22:04:41 +00:00
|
|
|
|
CreateHostBuilder(args, ConfigureDefault, ConfigureDefaultNlog).Build().Run();
|
2021-10-08 23:48:07 +01:00
|
|
|
|
}
|
2021-10-11 01:06:43 +01:00
|
|
|
|
|
2021-11-03 17:38:54 +00:00
|
|
|
|
public static RootOptions ConfigureOptions(HostBuilderContext context, IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.Configure<RootOptions>(options =>
|
|
|
|
|
{
|
|
|
|
|
OptionsHelper.ConfigureOptions(options, context.Configuration);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-11 08:11:55 +00:00
|
|
|
|
var config = OptionsHelper.ConfigureOptions(context.Configuration);
|
|
|
|
|
|
|
|
|
|
services.Configure<SpotifyAppCredentials>(options =>
|
|
|
|
|
{
|
|
|
|
|
options.ClientId = config.ClientId;
|
|
|
|
|
options.ClientSecret = config.ClientSecret;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return config;
|
2021-11-03 17:38:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ConfigureLastFm(RootOptions config, IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
if(config.LastfmClient is not null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> Adding Last.fm credentials...");
|
|
|
|
|
|
2021-11-29 21:48:00 +00:00
|
|
|
|
services.AddLastFm(config.LastfmClient, config.LastfmSecret);
|
2021-12-04 12:49:09 +00:00
|
|
|
|
|
|
|
|
|
if(config.RedisOptions.Enabled)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> Adding caching Last.fm consumers...");
|
|
|
|
|
services.AddCachingLastFm();
|
|
|
|
|
}
|
2021-11-03 17:38:54 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> No Last.fm credentials, skipping init...");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ConfigureDb(RootOptions config, IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
if (config.DatabaseOptions.Enabled)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> Adding Databse Context...");
|
|
|
|
|
services.AddDbContext<ApplicationDbContext>(options =>
|
|
|
|
|
options.UseNpgsql(config.DatabaseOptions.ConnectionString)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ConfigureEqual(RootOptions config, IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
switch (config.Equality)
|
|
|
|
|
{
|
|
|
|
|
case EqualityChecker.Uri:
|
|
|
|
|
Console.WriteLine("> Using Uri Equality");
|
|
|
|
|
services.AddSingleton<IEqual, UriEqual>();
|
|
|
|
|
break;
|
|
|
|
|
case EqualityChecker.String:
|
|
|
|
|
Console.WriteLine("> Using String Equality");
|
|
|
|
|
services.AddSingleton<IEqual, StringEqual>();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ConfigureDefault(HostBuilderContext context, IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("~~~ Selector CLI ~~~");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("> Configuring...");
|
|
|
|
|
// CONFIG
|
|
|
|
|
var config = ConfigureOptions(context, services);
|
2021-12-19 13:44:22 +00:00
|
|
|
|
services.AddHttpClient();
|
2021-11-03 17:38:54 +00:00
|
|
|
|
|
|
|
|
|
Console.WriteLine("> Adding Services...");
|
|
|
|
|
// SERVICES
|
2021-11-29 21:48:00 +00:00
|
|
|
|
services.AddConsumerFactories();
|
2021-12-04 12:49:09 +00:00
|
|
|
|
if (config.RedisOptions.Enabled)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> Adding caching consumers...");
|
|
|
|
|
services.AddCachingConsumerFactories();
|
|
|
|
|
}
|
2021-11-29 21:48:00 +00:00
|
|
|
|
services.AddWatcher();
|
2021-11-29 21:04:15 +00:00
|
|
|
|
|
2021-12-19 18:44:03 +00:00
|
|
|
|
services.AddEvents();
|
2021-12-04 12:49:09 +00:00
|
|
|
|
|
2021-12-19 18:44:03 +00:00
|
|
|
|
services.AddSpotify();
|
2021-11-03 17:38:54 +00:00
|
|
|
|
ConfigureLastFm(config, services);
|
|
|
|
|
ConfigureDb(config, services);
|
2021-12-19 18:44:03 +00:00
|
|
|
|
ConfigureEqual(config, services);
|
2021-11-29 21:04:15 +00:00
|
|
|
|
|
2021-12-19 18:44:03 +00:00
|
|
|
|
if (config.RedisOptions.Enabled)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> Adding Redis...");
|
2021-11-29 21:04:15 +00:00
|
|
|
|
services.AddRedisServices(config.RedisOptions.ConnectionString);
|
|
|
|
|
|
2021-12-19 18:44:03 +00:00
|
|
|
|
Console.WriteLine("> Adding cache event maps...");
|
2021-12-20 23:04:53 +00:00
|
|
|
|
services.AddTransient<IEventMapping, FromPubSub.SpotifyLink>();
|
|
|
|
|
services.AddTransient<IEventMapping, FromPubSub.Lastfm>();
|
2021-12-19 18:44:03 +00:00
|
|
|
|
|
|
|
|
|
Console.WriteLine("> Adding caching Spotify consumers...");
|
|
|
|
|
services.AddCachingSpotify();
|
|
|
|
|
}
|
2021-11-03 17:38:54 +00:00
|
|
|
|
|
|
|
|
|
// HOSTED SERVICES
|
|
|
|
|
if (config.WatcherOptions.Enabled)
|
|
|
|
|
{
|
2021-11-25 18:26:20 +00:00
|
|
|
|
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>();
|
|
|
|
|
}
|
2021-11-03 17:38:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ConfigureDefaultNlog(HostBuilderContext context, ILoggingBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
builder.ClearProviders();
|
|
|
|
|
builder.SetMinimumLevel(LogLevel.Trace);
|
|
|
|
|
builder.AddNLog(context.Configuration);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static IHostBuilder CreateHostBuilder(string[] args, Action<HostBuilderContext, IServiceCollection> BuildServices, Action<HostBuilderContext, ILoggingBuilder> BuildLogs)
|
|
|
|
|
=> Host.CreateDefaultBuilder(args)
|
2021-12-04 12:49:09 +00:00
|
|
|
|
.UseWindowsService()
|
|
|
|
|
.UseSystemd()
|
2021-11-03 17:38:54 +00:00
|
|
|
|
.ConfigureServices((context, services) => BuildServices(context, services))
|
|
|
|
|
.ConfigureLogging((context, builder) => BuildLogs(context, builder));
|
2021-10-08 23:48:07 +01:00
|
|
|
|
}
|
|
|
|
|
}
|