Files
Open-Archival/OpenArchival.Blazor/Program.cs
2026-05-17 20:54:09 -04:00

172 lines
6.5 KiB
C#

///TODO:
/// - Refine the home page/about page editor to use the API instead of embeding images
/// - Add auth back to the admin pages
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using MudBlazor.Services;
using MyAppName.WebApp.Components.Account;
using OpenArchival.Blazor;
using OpenArchival.Blazor.AdminPages.Shared;
using OpenArchival.Blazor.ArchiveDisplay;
using OpenArchival.Blazor.Components;
using OpenArchival.Blazor.Config;
using OpenArchival.Blazor.CustomComponents;
using OpenArchival.DataAccess;
using OpenArchival.DataAccess.FileAccessManager;
var builder = WebApplication.CreateBuilder(args);
var uploadSettings = builder.Configuration.GetSection(FileUploadOptions.Key).Get<FileUploadOptions>() ?? throw new ArgumentNullException("FileUploadOptions");
// --- UI Services ---
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents()
.AddHubOptions(options =>
{
options.MaximumReceiveMessageSize = 1000 * 1024 * 1024;
options.ClientTimeoutInterval = TimeSpan.FromSeconds(60);
options.HandshakeTimeout = TimeSpan.FromSeconds(30);
});
builder.Services.AddMudServices();
builder.Services.AddMudExtensions();
builder.Services.AddControllers();
builder.Services.AddHttpClient();
// --- Database & Identity Configuration ---
// Get the single connection string for your PostgreSQL database.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
// Use AddDbContextFactory for Blazor Server. This is safer for managing DbContext lifecycles.
// This single ApplicationDbContext will handle both Identity and your application data.
builder.Services.AddDbContextFactory<ApplicationDbContext>(options =>
options.UseNpgsql(connectionString), ServiceLifetime.Scoped);
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
// Configure Identity to use ApplicationDbContext, which is pointed at PostgreSQL.
builder.Services.AddIdentityCore<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddSignInManager()
.AddDefaultTokenProviders();
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddScoped<IdentityUserAccessor>();
builder.Services.AddScoped<IdentityRedirectManager>();
builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();
builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>();
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = IdentityConstants.ApplicationScheme;
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
})
.AddIdentityCookies();
// File Upload Configuration
builder.Services.AddOptions<FileUploadOptions>().Bind(builder.Configuration.GetSection(FileUploadOptions.Key));
builder.Services.AddOptions<ApplicationOptions>().Bind(builder.Configuration.GetSection(ApplicationOptions.Key));
// Custom Application Services
builder.Services.AddScoped<IArchiveCategoryProvider, ArchiveCategoryProvider>();
builder.Services.AddScoped<IFilePathListingProvider, FilePathListingProvider>();
builder.Services.AddScoped<IArtifactStorageLocationProvider, ArtifactStorageLocationProvider>();
builder.Services.AddScoped<IArtifactDefectProvider, ArtifactDefectProvider>();
builder.Services.AddScoped<IArtifactTypeProvider, ArtifactTypeProvider>();
builder.Services.AddScoped<IArchiveEntryTagProvider, ArchiveEntryTagProvider>();
builder.Services.AddScoped<IListedNameProvider, ListedNameProvider>();
builder.Services.AddScoped<ArtifactEntrySharedHelpers>();
builder.Services.AddScoped<IArtifactGroupingProvider, ArtifactGroupingProvider>();
builder.Services.AddScoped<ArtifactGroupingSearch>();
builder.Services.AddScoped<BlogPostSearch>();
builder.Services.AddScoped<IFileAccessManager, FileAccessManager>();
builder.Services.AddScoped<IImageThumbnailer, ImageThumbnailer>();
builder.Services.AddLogging();
builder.Services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo("/app/keys"))
.SetApplicationName("OpenArchival");
builder.Services.AddMemoryCache();
var app = builder.Build();
app.MapControllers();
using (var scope = app.Services.CreateScope())
{
var serviceProvider = scope.ServiceProvider;
var logger = scope.ServiceProvider.GetService<ILogger<Program>>();
if (logger is null)
{
Console.WriteLine("Logger not found!");
System.Environment.Exit(1);
}
// First migrate the database if there is a new migration available
try
{
logger.LogInformation("Migrating database...");
var factory = serviceProvider.GetRequiredService<IDbContextFactory<ApplicationDbContext>>();
await using var context = await factory.CreateDbContextAsync();
await context.Database.MigrateAsync();
}
catch (Exception ex)
{
logger.LogError($"Failed to migrate database. Got error: {ex.Message}\n");
System.Environment.Exit(1);
}
// Add the admin user
try
{
await IdentityDataSeeder.SeedRolesAndAdminUserAsync(serviceProvider);
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred while seeding the database with admin credentials.");
}
// Seed the home page configuration
await HomepageConfigurationSeeder.SeedHomepageConfig(serviceProvider);
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAntiforgery();
app.MapRazorComponents<App>()
.AddAdditionalAssemblies(typeof(OpenArchival.Blazor.AdminPages.ArchiveManagement).Assembly,
typeof(OpenArchival.Blazor.ArtifactGroupingDisplay.ArchiveGroupingDisplay).Assembly,
typeof(OpenArchival.Blazor.ArchiveDisplay.SearchArchive).Assembly,
typeof(OpenArchival.Blazor.Blog.BlogEditor).Assembly)
.AddInteractiveServerRenderMode();
app.MapAdditionalIdentityEndpoints();
app.MapPost("/Logout", async (SignInManager<OpenArchival.DataAccess.ApplicationUser> signInManager) =>
{
await signInManager.SignOutAsync();
// Redirects the user back to the home page
return Microsoft.AspNetCore.Http.Results.Redirect("/");
}).DisableAntiforgery();
app.Run();