SharpRSS/SharpRSS.Blazor/Program.cs

83 lines
2.5 KiB
C#
Raw Permalink Normal View History

using Blazored.LocalStorage;
using DotBased.ASP.Auth;
using DotBased.ASP.Auth.Domains.Auth;
using DotBased.ASP.Auth.Domains.Identity;
using Microsoft.EntityFrameworkCore;
2024-06-17 13:53:48 +02:00
using MudBlazor.Services;
using SharpRSS.Blazor.Auth;
using SharpRSS.Blazor.Components;
using SharpRSS.Business;
using SharpRSS.Data;
2024-07-13 16:29:27 +02:00
using SharpRSS.Data.Domains.Configuration;
var builder = WebApplication.CreateBuilder(args);
builder.AddSRSS();
2024-07-06 00:02:02 +02:00
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();
2024-06-17 13:53:48 +02:00
builder.Services.AddMudServices();
builder.Services.AddHttpContextAccessor(); // HttpContext accessor
2024-07-06 00:02:02 +02:00
/*
* HSTS config
*/
2024-07-13 16:29:27 +02:00
var hstsConfig = new HstsConfiguration();
var configSection = builder.Configuration.GetSection($"HTTP:{HstsConfiguration.Hsts}");
configSection.Bind(hstsConfig);
if (hstsConfig.EnableHsts)
2024-07-06 00:02:02 +02:00
{
2024-07-13 16:29:27 +02:00
builder.Services.AddHsts(options =>
{
options.Preload = hstsConfig.Preload;
options.IncludeSubDomains = hstsConfig.IncludeSubdomains;
options.MaxAge = TimeSpan.FromSeconds(hstsConfig.MaxAgeSeconds);
});
}
2024-07-06 00:02:02 +02:00
var app = builder.Build();
2024-07-13 16:29:27 +02:00
//TODO: Move to migrations
var contextFactory = app.Services.GetService<IDbContextFactory<SRSSContext>>();
if (contextFactory != null)
{
await using var context = await contextFactory.CreateDbContextAsync();
context.Database.EnsureCreated();
}
2024-07-06 00:02:02 +02:00
else
{
}
if (!app.Environment.IsDevelopment())
{
2024-07-13 16:29:27 +02:00
app.UseDeveloperExceptionPage();
app.UseExceptionHandler("/Error", createScopeForErrors: true);
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAntiforgery();
app.UseBasedServerAuth();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.Run();