2021-10-18 21:48:02 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
2021-10-24 00:23:45 +01:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
2021-10-23 22:16:37 +01:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
2021-10-31 19:47:07 +00:00
|
|
|
using Selector.Web.Hubs;
|
2021-11-29 21:48:00 +00:00
|
|
|
using Selector.Web.Extensions;
|
|
|
|
using Selector.Extensions;
|
2021-10-23 22:16:37 +01:00
|
|
|
using Selector.Model;
|
2021-11-29 21:04:15 +00:00
|
|
|
using Selector.Model.Extensions;
|
2021-10-27 23:00:01 +01:00
|
|
|
using Selector.Cache;
|
2021-11-29 21:04:15 +00:00
|
|
|
using Selector.Cache.Extensions;
|
2021-10-23 22:16:37 +01:00
|
|
|
|
2021-10-18 21:48:02 +01:00
|
|
|
namespace Selector.Web
|
|
|
|
{
|
|
|
|
public class Startup
|
|
|
|
{
|
|
|
|
public Startup(IConfiguration configuration)
|
|
|
|
{
|
|
|
|
Configuration = configuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
{
|
2021-10-27 17:31:29 +01:00
|
|
|
services.Configure<RootOptions>(options =>
|
|
|
|
{
|
|
|
|
OptionsHelper.ConfigureOptions(options, Configuration);
|
|
|
|
});
|
2021-10-27 23:00:01 +01:00
|
|
|
var config = OptionsHelper.ConfigureOptions(Configuration);
|
2021-10-27 17:31:29 +01:00
|
|
|
|
2021-11-11 08:11:55 +00:00
|
|
|
services.Configure<SpotifyAppCredentials>(options =>
|
|
|
|
{
|
|
|
|
options.ClientId = config.ClientId;
|
|
|
|
options.ClientSecret = config.ClientSecret;
|
|
|
|
});
|
|
|
|
|
2021-10-23 22:16:37 +01:00
|
|
|
services.AddRazorPages().AddRazorRuntimeCompilation();
|
2021-10-23 12:49:42 +01:00
|
|
|
services.AddControllers();
|
2021-11-10 23:54:28 +00:00
|
|
|
services.AddSignalR(o => o.EnableDetailedErrors = true);
|
2021-10-23 22:16:37 +01:00
|
|
|
|
2021-10-24 20:15:09 +01:00
|
|
|
services.AddDbContext<ApplicationDbContext>(options =>
|
2021-10-23 22:16:37 +01:00
|
|
|
options.UseNpgsql(Configuration.GetConnectionString("Default"))
|
|
|
|
);
|
2021-10-24 00:23:45 +01:00
|
|
|
|
2021-10-24 20:15:09 +01:00
|
|
|
services.AddIdentity<ApplicationUser, IdentityRole>()
|
|
|
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
2021-10-24 00:23:45 +01:00
|
|
|
.AddDefaultUI()
|
|
|
|
.AddDefaultTokenProviders();
|
|
|
|
|
|
|
|
services.Configure<IdentityOptions>(options =>
|
|
|
|
{
|
|
|
|
// Password settings.
|
2021-10-24 17:38:45 +01:00
|
|
|
options.Password.RequireDigit = false;
|
|
|
|
options.Password.RequireLowercase = false;
|
|
|
|
options.Password.RequireNonAlphanumeric = false;
|
|
|
|
options.Password.RequireUppercase = false;
|
2021-10-24 00:23:45 +01:00
|
|
|
options.Password.RequiredLength = 3;
|
|
|
|
options.Password.RequiredUniqueChars = 1;
|
|
|
|
|
|
|
|
// Lockout settings.
|
|
|
|
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
|
|
|
|
options.Lockout.MaxFailedAccessAttempts = 5;
|
|
|
|
options.Lockout.AllowedForNewUsers = true;
|
|
|
|
|
|
|
|
// User settings.
|
|
|
|
options.User.AllowedUserNameCharacters =
|
|
|
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
|
|
|
|
options.User.RequireUniqueEmail = false;
|
|
|
|
options.SignIn.RequireConfirmedEmail = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
services.ConfigureApplicationCookie(options =>
|
|
|
|
{
|
|
|
|
// Cookie settings
|
|
|
|
options.Cookie.HttpOnly = true;
|
2021-11-09 20:58:02 +00:00
|
|
|
options.ExpireTimeSpan = TimeSpan.FromDays(1);
|
2021-10-24 00:23:45 +01:00
|
|
|
|
|
|
|
options.LoginPath = "/Identity/Account/Login";
|
|
|
|
options.AccessDeniedPath = "/Identity/Account/AccessDenied";
|
|
|
|
options.SlidingExpiration = true;
|
|
|
|
});
|
2021-10-24 20:15:09 +01:00
|
|
|
|
2021-11-29 21:04:15 +00:00
|
|
|
services.AddAuthorisationHandlers();
|
2021-10-26 19:26:41 +01:00
|
|
|
|
2021-10-27 23:00:01 +01:00
|
|
|
if (config.RedisOptions.Enabled)
|
2021-11-29 21:04:15 +00:00
|
|
|
services.AddRedisServices(config.RedisOptions.ConnectionString);
|
2021-11-03 22:27:50 +00:00
|
|
|
|
2021-11-29 21:48:00 +00:00
|
|
|
services.AddSpotify();
|
2021-12-04 12:49:09 +00:00
|
|
|
if (config.RedisOptions.Enabled)
|
|
|
|
{
|
|
|
|
Console.WriteLine("> Adding caching Spotify consumers...");
|
|
|
|
services.AddCachingSpotify();
|
|
|
|
services.AddCacheHubProxy();
|
|
|
|
}
|
2021-11-10 01:46:30 +00:00
|
|
|
|
2021-11-30 20:38:26 +00:00
|
|
|
ConfigureLastFm(config, services);
|
2021-10-18 21:48:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
|
|
{
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
app.UseExceptionHandler("/Error");
|
|
|
|
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
|
|
|
app.UseHsts();
|
|
|
|
}
|
|
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
2021-10-24 00:23:45 +01:00
|
|
|
app.UseAuthentication();
|
2021-10-18 21:48:02 +01:00
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
{
|
|
|
|
endpoints.MapRazorPages();
|
2021-10-23 12:49:42 +01:00
|
|
|
endpoints.MapControllers();
|
2021-10-31 19:47:07 +00:00
|
|
|
endpoints.MapHub<NowPlayingHub>("/hub");
|
2021-10-18 21:48:02 +01:00
|
|
|
});
|
|
|
|
}
|
2021-11-30 20:38:26 +00:00
|
|
|
|
|
|
|
public static void ConfigureLastFm(RootOptions config, IServiceCollection services)
|
|
|
|
{
|
|
|
|
if (config.LastfmClient is not null)
|
|
|
|
{
|
|
|
|
Console.WriteLine("> Adding Last.fm credentials...");
|
|
|
|
|
|
|
|
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-30 20:38:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Console.WriteLine("> No Last.fm credentials, skipping init...");
|
|
|
|
}
|
|
|
|
}
|
2021-10-18 21:48:02 +01:00
|
|
|
}
|
|
|
|
}
|