SharpRSS/SharpRSS.Blazor/Program.cs

83 lines
2.5 KiB
C#

using Blazored.LocalStorage;
using DotBased.ASP.Auth;
using DotBased.ASP.Auth.Domains.Auth;
using DotBased.ASP.Auth.Domains.Identity;
using Microsoft.EntityFrameworkCore;
using MudBlazor.Services;
using SharpRSS.Blazor.Auth;
using SharpRSS.Blazor.Components;
using SharpRSS.Business;
using SharpRSS.Data;
using SharpRSS.Data.Domains.Configuration;
var builder = WebApplication.CreateBuilder(args);
builder.AddSRSS();
builder.Services.AddBlazoredLocalStorage();
builder.Services.AddBasedServerAuth(options =>
{
options.AllowRegistration = false;
options.AuthenticationStateMaxAgeBeforeExpire = TimeSpan.FromDays(7);
options.LoginPath = "/auth/login";
options.LogoutPath = "/auth/logout";
options.SeedData = service =>
{
service.CreateUserAsync(new UserModel() { UserName = "Admin", Email = "admin@example.com", Enabled = true, PasswordHash = "password", Roles = new List<RoleModel>() { new RoleModel() { Name = "Admin", Description = "Administration role." }}});
service.CreateUserAsync(new UserModel() { UserName = "User", Email = "user@example.com", Enabled = true, PasswordHash = "password"});
};
options.SetDataProviderType<MemoryAuthDataProvider>();
options.SetSessionStateProviderType<LocalStorageSessionStateProvider>();
});
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.AddMudServices();
builder.Services.AddHttpContextAccessor(); // HttpContext accessor
/*
* HSTS config
*/
var hstsConfig = new HstsConfiguration();
var configSection = builder.Configuration.GetSection($"HTTP:{HstsConfiguration.Hsts}");
configSection.Bind(hstsConfig);
if (hstsConfig.EnableHsts)
{
builder.Services.AddHsts(options =>
{
options.Preload = hstsConfig.Preload;
options.IncludeSubDomains = hstsConfig.IncludeSubdomains;
options.MaxAge = TimeSpan.FromSeconds(hstsConfig.MaxAgeSeconds);
});
}
var app = builder.Build();
//TODO: Move to migrations
var contextFactory = app.Services.GetService<IDbContextFactory<SRSSContext>>();
if (contextFactory != null)
{
await using var context = await contextFactory.CreateDbContextAsync();
context.Database.EnsureCreated();
}
else
{
}
if (!app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseExceptionHandler("/Error", createScopeForErrors: true);
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAntiforgery();
app.UseBasedServerAuth();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.Run();