Wave/Wave/Program.cs

94 lines
2.9 KiB
C#
Raw Normal View History

2024-01-11 12:54:29 +00:00
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Server;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Wave.Components;
using Wave.Components.Account;
using Wave.Data;
using Wave.Services;
2024-01-11 12:54:29 +00:00
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddEnvironmentVariables("WAVE_");
2024-01-11 12:54:29 +00:00
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
builder.Services.AddControllers();
2024-01-11 12:54:29 +00:00
2024-01-11 14:39:09 +00:00
#region Authentication & Authorization
2024-01-11 12:54:29 +00:00
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddScoped<IdentityUserAccessor>();
builder.Services.AddScoped<IdentityRedirectManager>();
builder.Services.AddScoped<AuthenticationStateProvider, ServerAuthenticationStateProvider>();
builder.Services.AddAuthorization();
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = IdentityConstants.ApplicationScheme;
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
})
.AddIdentityCookies();
2024-01-11 14:39:09 +00:00
#endregion
#region Identity
string connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
2024-01-14 18:04:06 +00:00
builder.Services.AddDbContextFactory<ApplicationDbContext>(options =>
options.UseNpgsql(connectionString));
2024-01-11 12:54:29 +00:00
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddIdentityCore<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddSignInManager()
.AddDefaultTokenProviders();
builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>();
2024-01-11 14:39:09 +00:00
#endregion
#region Services
builder.Services.AddLocalization(options => {
options.ResourcesPath = "Resources";
});
builder.Services.AddScoped<ImageService>();
2024-01-11 14:39:09 +00:00
#endregion
2024-01-11 12:54:29 +00:00
var app = builder.Build();
// Configure the HTTP request pipeline.
2024-01-11 14:39:09 +00:00
if (app.Environment.IsDevelopment()) {
2024-01-11 12:54:29 +00:00
app.UseMigrationsEndPoint();
2024-01-11 14:39:09 +00:00
} else {
2024-01-11 12:54:29 +00:00
app.UseExceptionHandler("/Error", createScopeForErrors: true);
}
app.UseStaticFiles();
app.UseAntiforgery();
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
2024-01-11 12:54:29 +00:00
// Add additional endpoints required by the Identity /Account Razor components.
app.MapAdditionalIdentityEndpoints();
app.MapControllers();
2024-01-11 14:39:09 +00:00
string[] cultures = ["en-US", "en-GB", "de-DE"];
app.UseRequestLocalization(new RequestLocalizationOptions {
ApplyCurrentCultureToResponseHeaders = true,
FallBackToParentCultures = true, FallBackToParentUICultures = true
}
.SetDefaultCulture(cultures[0])
.AddSupportedCultures(cultures)
.AddSupportedUICultures(cultures));
2024-01-15 19:47:10 +00:00
{
using var scope = app.Services.CreateScope();
using var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
context.Database.Migrate();
}
2024-01-11 12:54:29 +00:00
app.Run();