Selector/Selector.Web/Startup.cs

167 lines
6.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2021-10-24 00:23:45 +01:00
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authorization;
2021-10-23 22:16:37 +01:00
using Microsoft.EntityFrameworkCore;
using StackExchange.Redis;
2021-11-03 22:27:50 +00:00
using Selector.Web.Service;
2021-10-31 19:47:07 +00:00
using Selector.Web.Hubs;
2021-10-23 22:16:37 +01:00
using Selector.Model;
using Selector.Model.Authorisation;
using Selector.Cache;
2021-10-23 22:16:37 +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)
{
services.Configure<RootOptions>(options =>
{
OptionsHelper.ConfigureOptions(options, Configuration);
});
var config = OptionsHelper.ConfigureOptions(Configuration);
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();
services.AddSignalR(o => o.EnableDetailedErrors = true);
2021-10-23 22:16:37 +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
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
2021-10-24 00:23:45 +01:00
.AddDefaultUI()
.AddDefaultTokenProviders();
services.Configure<IdentityOptions>(options =>
{
// Password settings.
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;
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-31 19:47:07 +00:00
// AUTH
services.AddAuthorization(options =>
{
options.FallbackPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
});
services.AddScoped<IAuthorizationHandler, WatcherIsOwnerAuthHandler>();
services.AddSingleton<IAuthorizationHandler, WatcherIsAdminAuthHandler>();
services.AddScoped<IAuthorizationHandler, UserIsSelfAuthHandler>();
services.AddSingleton<IAuthorizationHandler, UserIsAdminAuthHandler>();
2021-10-31 19:47:07 +00:00
// REDIS
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);
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-11-03 22:27:50 +00:00
services.AddSingleton<IRefreshTokenFactoryProvider, CachingRefreshTokenFactoryProvider>();
services.AddSingleton<AudioFeaturePuller>();
2021-11-03 22:27:50 +00:00
services.AddSingleton<CacheHubProxy>();
2021-11-05 07:58:48 +00:00
services.AddHostedService<CacheHubProxyService>();
services.AddTransient<INowPlayingMappingFactory, NowPlayingMappingFactory>();
services.AddScoped<IUserMapping, NowPlayingUserMapping>();
}
// 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();
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");
});
}
}
}