Iceshrimp.NET/Iceshrimp.Backend/Pages/Shared/RootComponent.razor
Laura Hausmann 705e061f74
[backend/asp] Refactor middleware stack
This commit splits the request pipeline conditionally instead of invoking every middleware in the stack.

It also simplifies middleware instantiation by using runtime discovery, allowing for Plugins to add Middleware.
2024-11-18 19:02:44 +01:00

73 lines
No EOL
2.5 KiB
Text

@using Iceshrimp.Backend.Core.Configuration
@using Iceshrimp.Backend.Core.Database.Tables
@using Iceshrimp.Backend.Core.Extensions
@using Iceshrimp.Backend.Core.Middleware
@using Iceshrimp.Backend.Core.Services
@using Microsoft.Extensions.Options
@using Microsoft.AspNetCore.Components.Routing
@inject IOptions<Config.InstanceSection> Instance
@preservewhitespace true
@attribute [BlazorSsr]
@inherits AsyncComponentBase
<!DOCTYPE html>
<html lang="en">
<!--suppress HtmlRequiredTitleElement, Justification: HeadOutlet -->
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<VersionedLink rel="stylesheet" href="/Iceshrimp.Backend.styles.css"/>
<VersionedLink rel="stylesheet" href="/css/default.css"/>
<VersionedLink rel="icon" type="image/png" href="/favicon.png"/>
<HeadOutlet/>
<PageTitle>@(_instanceName ?? "Iceshrimp.NET")</PageTitle>
</head>
<body>
<Router AppAssembly="@typeof(RootComponent).Assembly">
<Found Context="routeData">
<CascadingValue Value="_user">
<CascadingValue Value="_instanceName" Name="InstanceName">
<RouteView RouteData="@routeData"/>
</CascadingValue>
</CascadingValue>
<footer>
@if (_instanceName != null)
{
<span>@_instanceName - </span>
}
<strong>Iceshrimp.NET</strong> v@(Instance.Value.Version)
<span class="float-right">
@if (_user == null)
{
<a href="/login?rd=@((Context.Request.Path.Value ?? "/").UrlEncode())">Login</a>
}
else
{
<span>Authenticated as <i>@_user.Username</i></span>
}
</span>
</footer>
</Found>
</Router>
</body>
</html>
@code {
[Inject] public required MetaService Meta { get; set; }
// This is separate from AuthorizationMiddleware.AuthorizeAttribute, as that middleware is meant for API calls.
public class RequireAuthorizationAttribute : Attribute;
private string? _instanceName;
private User? _user;
protected override async Task OnParametersSetAsync()
{
_instanceName = await Meta.Get(MetaEntity.InstanceName);
_user = Context.GetUser();
if (Context.GetEndpoint()?.Metadata.GetMetadata<RequireAuthorizationAttribute>() == null)
return;
if (_user == null)
RedirectToLogin();
}
}