Files
SpotiParty/SpotiParty.Web/Program.cs

104 lines
3.3 KiB
C#

using HopFrame.Core.Callbacks;
using HopFrame.Core.Services;
using HopFrame.Web;
using Microsoft.EntityFrameworkCore;
using SpotiParty.Web;
using SpotiParty.Web.Components;
using SpotiParty.Web.Models;
using SpotiParty.Web.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.AddServiceDefaults();
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<ClientSideStorage>();
builder.AddNpgsqlDbContext<DatabaseContext>("SpotiParty");
builder.Services.AddDbContextFactory<DatabaseContext>(options => {
options.UseNpgsql(builder.Configuration.GetConnectionString("SpotiParty"));
});
builder.Services.AddScoped<AuthorizationHandler>();
builder.Services.AddScoped<IHopFrameAuthHandler, DashboardAuthHandler>();
builder.Services.AddScoped<DashboardAuthHandler>();
builder.Services.AddScoped<EventsDashboardRepo>();
builder.Services.AddHopFrame(config => {
config.SetLoginPage("/login");
config.AddDbContext<DatabaseContext>(context => {
context.Table<User>(table => {
table
.SetViewPolicy(DashboardAuthHandler.AdminPolicy)
.SetCreatePolicy(DashboardAuthHandler.AdminPolicy)
.SetUpdatePolicy(DashboardAuthHandler.AdminPolicy)
.SetDeletePolicy(DashboardAuthHandler.AdminPolicy);
table.Property(u => u.RefreshToken)
.List(false)
.DisplayValue(false)
.SetEditable(false);
table.ShowSearchSuggestions(false);
});
context.Table<Event>()
.Ignore(true);
});
config.AddCustomRepository<EventsDashboardRepo, Event, Guid>(e => e.Id, table => {
//table.SetDisplayName("Events");
table.Property(e => e.Id)
.List(false)
.SetEditable(false)
.SetCreatable(false);
table.Property(e => e.Host)
.SetEditable(false)
.SetCreatable(false)
.SetDisplayedProperty(u => u.DisplayName);
table.ShowSearchSuggestions(false);
table.AddCallbackHandler(CallbackType.CreateEntry, async (entry, services) => {
var auth = services.GetRequiredService<DashboardAuthHandler>();
var user = await auth.GetCurrentUser();
entry.Host = user!;
});
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) {
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
await using (var scope = app.Services.CreateAsyncScope()) {
var context = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
await context.Database.MigrateAsync();
}
app.MapDefaultEndpoints();
app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
//app.UseHttpsRedirection();
app.UseAntiforgery();
app.MapStaticAssets();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode()
.AddHopFramePages();
app.Run();