2021-10-08 23:48:07 +01:00
|
|
|
|
using System;
|
2021-10-11 01:06:43 +01:00
|
|
|
|
using System.Threading.Tasks;
|
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-11 01:06:43 +01:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
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-10-24 17:38:45 +01:00
|
|
|
|
using Selector.Model;
|
2021-10-27 23:00:01 +01:00
|
|
|
|
using Selector.Cache;
|
2021-10-31 08:54:11 +00:00
|
|
|
|
using IF.Lastfm.Core.Api;
|
2021-10-27 23:00:01 +01:00
|
|
|
|
using StackExchange.Redis;
|
2021-10-24 17:38:45 +01:00
|
|
|
|
|
2021-10-08 23:48:07 +01:00
|
|
|
|
namespace Selector.CLI
|
|
|
|
|
{
|
|
|
|
|
class Program
|
|
|
|
|
{
|
2021-10-11 01:06:43 +01:00
|
|
|
|
public static async Task Main(string[] args)
|
2021-10-08 23:48:07 +01:00
|
|
|
|
{
|
2021-10-11 01:06:43 +01:00
|
|
|
|
var host = CreateHostBuilder(args);
|
|
|
|
|
await host.RunConsoleAsync();
|
2021-10-08 23:48:07 +01:00
|
|
|
|
}
|
2021-10-11 01:06:43 +01:00
|
|
|
|
|
|
|
|
|
static IHostBuilder CreateHostBuilder(string[] args)
|
|
|
|
|
=> Host.CreateDefaultBuilder(args)
|
2021-10-24 17:38:45 +01:00
|
|
|
|
.ConfigureServices((context, services) =>
|
|
|
|
|
{
|
2021-10-13 23:19:47 +01:00
|
|
|
|
Console.WriteLine("~~~ Selector CLI ~~~");
|
2021-10-24 17:38:45 +01:00
|
|
|
|
Console.WriteLine();
|
2021-10-13 23:19:47 +01:00
|
|
|
|
|
2021-10-15 19:58:07 +01:00
|
|
|
|
Console.WriteLine("> Configuring...");
|
2021-10-11 18:17:36 +01:00
|
|
|
|
// CONFIG
|
2021-10-24 17:38:45 +01:00
|
|
|
|
services.Configure<RootOptions>(options =>
|
|
|
|
|
{
|
|
|
|
|
OptionsHelper.ConfigureOptions(options, context.Configuration);
|
2021-10-11 23:19:19 +01:00
|
|
|
|
});
|
2021-10-24 17:38:45 +01:00
|
|
|
|
var config = OptionsHelper.ConfigureOptions(context.Configuration);
|
2021-10-11 18:17:36 +01:00
|
|
|
|
|
2021-10-15 19:58:07 +01:00
|
|
|
|
Console.WriteLine("> Adding Services...");
|
2021-10-11 18:17:36 +01:00
|
|
|
|
// SERVICES
|
2021-10-12 23:29:05 +01:00
|
|
|
|
services.AddSingleton<IWatcherFactory, WatcherFactory>();
|
2021-10-28 23:05:07 +01:00
|
|
|
|
services.AddSingleton<IAudioFeatureInjectorFactory, AudioFeatureInjectorFactory>();
|
2021-10-12 23:29:05 +01:00
|
|
|
|
services.AddSingleton<IWatcherCollectionFactory, WatcherCollectionFactory>();
|
|
|
|
|
// For generating spotify clients
|
2021-10-15 19:58:07 +01:00
|
|
|
|
//services.AddSingleton<IRefreshTokenFactoryProvider, RefreshTokenFactoryProvider>();
|
|
|
|
|
services.AddSingleton<IRefreshTokenFactoryProvider, CachingRefreshTokenFactoryProvider>();
|
2021-10-11 23:19:19 +01:00
|
|
|
|
|
2021-10-31 08:54:11 +00:00
|
|
|
|
if(config.LastfmClient is not null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> Adding Last.fm credentials...");
|
|
|
|
|
|
|
|
|
|
var lastAuth = new LastAuth(config.LastfmClient, config.LastfmSecret);
|
|
|
|
|
services.AddSingleton<LastAuth>(lastAuth);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> No Last.fm credentials, skipping init...");
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 21:26:57 +01:00
|
|
|
|
// DB
|
2021-10-24 17:38:45 +01:00
|
|
|
|
if (config.DatabaseOptions.Enabled)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> Adding Databse Context...");
|
2021-10-24 20:15:09 +01:00
|
|
|
|
services.AddDbContext<ApplicationDbContext>(options =>
|
2021-10-24 17:38:45 +01:00
|
|
|
|
options.UseNpgsql(config.DatabaseOptions.ConnectionString)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 21:26:57 +01:00
|
|
|
|
// REDIS
|
2021-10-27 23:00:01 +01:00
|
|
|
|
if (config.RedisOptions.Enabled)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> Configuring Redis...");
|
|
|
|
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(config.RedisOptions.ConnectionString))
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> No Redis configuration string provided, exiting...");
|
|
|
|
|
Environment.Exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var connMulti = ConnectionMultiplexer.Connect(config.RedisOptions.ConnectionString);
|
|
|
|
|
services.AddSingleton(connMulti);
|
2021-10-28 21:26:57 +01:00
|
|
|
|
services.AddTransient<IDatabaseAsync>(services => services.GetService<ConnectionMultiplexer>().GetDatabase());
|
2021-10-28 23:05:07 +01:00
|
|
|
|
services.AddTransient<ISubscriber>(services => services.GetService<ConnectionMultiplexer>().GetSubscriber());
|
2021-10-27 23:00:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 21:26:57 +01:00
|
|
|
|
// EQUAL
|
2021-10-24 17:38:45 +01:00
|
|
|
|
switch (config.Equality)
|
2021-10-11 23:19:19 +01:00
|
|
|
|
{
|
|
|
|
|
case EqualityChecker.Uri:
|
2021-10-15 19:58:07 +01:00
|
|
|
|
Console.WriteLine("> Using Uri Equality");
|
2021-10-11 23:19:19 +01:00
|
|
|
|
services.AddTransient<IEqual, UriEqual>();
|
|
|
|
|
break;
|
|
|
|
|
case EqualityChecker.String:
|
2021-10-15 19:58:07 +01:00
|
|
|
|
Console.WriteLine("> Using String Equality");
|
2021-10-11 23:19:19 +01:00
|
|
|
|
services.AddTransient<IEqual, StringEqual>();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-10-11 18:17:36 +01:00
|
|
|
|
|
|
|
|
|
// HOSTED SERVICES
|
2021-10-24 17:38:45 +01:00
|
|
|
|
if (config.WatcherOptions.Enabled)
|
2021-10-13 23:19:47 +01:00
|
|
|
|
{
|
2021-10-15 19:58:07 +01:00
|
|
|
|
Console.WriteLine("> Adding Watcher Service");
|
2021-10-13 23:19:47 +01:00
|
|
|
|
services.AddHostedService<WatcherService>();
|
|
|
|
|
}
|
2021-10-24 17:38:45 +01:00
|
|
|
|
|
2021-10-11 01:06:43 +01:00
|
|
|
|
})
|
2021-10-24 17:38:45 +01:00
|
|
|
|
.ConfigureLogging((context, builder) =>
|
|
|
|
|
{
|
2021-10-11 18:17:36 +01:00
|
|
|
|
builder.ClearProviders();
|
|
|
|
|
builder.SetMinimumLevel(LogLevel.Trace);
|
|
|
|
|
builder.AddNLog(context.Configuration);
|
2021-10-11 01:06:43 +01:00
|
|
|
|
});
|
2021-10-08 23:48:07 +01:00
|
|
|
|
}
|
|
|
|
|
}
|