From 73a4ac599c74fc10699a26d000327db167a992dd Mon Sep 17 00:00:00 2001 From: Nicolas Dorier Date: Wed, 13 Sep 2023 13:13:15 +0900 Subject: [PATCH] Add Blazor server (#5312) * Add Blazor server * Improve Blazor status UI * Improve UX --------- Co-authored-by: Dennis Reimann --- BTCPayServer/Blazor/_Imports.razor | 7 ++ BTCPayServer/Hosting/Startup.cs | 38 +++++++-- BTCPayServer/Views/Shared/LayoutFoot.cshtml | 6 +- BTCPayServer/Views/Shared/_Footer.cshtml | 16 ++++ .../Views/UIReports/StoreReports.cshtml | 2 +- BTCPayServer/wwwroot/main/layout.css | 22 +++++ BTCPayServer/wwwroot/main/site.css | 6 ++ BTCPayServer/wwwroot/main/site.js | 84 +++++++++++++++++++ 8 files changed, 171 insertions(+), 10 deletions(-) create mode 100644 BTCPayServer/Blazor/_Imports.razor diff --git a/BTCPayServer/Blazor/_Imports.razor b/BTCPayServer/Blazor/_Imports.razor new file mode 100644 index 000000000..9820bfde2 --- /dev/null +++ b/BTCPayServer/Blazor/_Imports.razor @@ -0,0 +1,7 @@ +@using System.Net.Http +@using Microsoft.AspNetCore.Authorization +@using Microsoft.AspNetCore.Components.Authorization +@using Microsoft.AspNetCore.Components.Forms +@using Microsoft.AspNetCore.Components.Routing +@using Microsoft.AspNetCore.Components.Web +@using Microsoft.JSInterop diff --git a/BTCPayServer/Hosting/Startup.cs b/BTCPayServer/Hosting/Startup.cs index 56d8cd536..a75e84a50 100644 --- a/BTCPayServer/Hosting/Startup.cs +++ b/BTCPayServer/Hosting/Startup.cs @@ -25,6 +25,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Rewrite; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -56,7 +57,6 @@ namespace BTCPayServer.Hosting } public ILoggerFactory LoggerFactory { get; } public Logs Logs { get; } - public void ConfigureServices(IServiceCollection services) { services.AddMemoryCache(); @@ -146,6 +146,8 @@ namespace BTCPayServer.Hosting .AddPlugins(services, Configuration, LoggerFactory) .AddControllersAsServices(); + services.AddServerSideBlazor(); + LowercaseTransformer.Register(services); ValidateControllerNameTransformer.Register(services); @@ -248,6 +250,13 @@ namespace BTCPayServer.Hosting rateLimits.SetZone($"zone={ZoneLimits.ForgotPassword} rate=5r/d burst=5 nodelay"); } + // HACK: blazor server js hard code some path, making it works only on root path. This fix it. + // Workaround this bug https://github.com/dotnet/aspnetcore/issues/43191 + var rewriteOptions = new RewriteOptions(); + rewriteOptions.AddRewrite("_blazor/(negotiate|initializers|disconnect)$", "/_blazor/$1", skipRemainingRules: true); + rewriteOptions.AddRewrite("_blazor$", "/_blazor", skipRemainingRules: true); + app.UseRewriter(rewriteOptions); + app.UseHeadersOverride(); var forwardingOptions = new ForwardedHeadersOptions() { @@ -264,15 +273,18 @@ namespace BTCPayServer.Hosting app.UseRouting(); app.UseCors(); + + // HACK: Make blazor js available on: ~/_blazorfiles/_framework/blazor.server.js + // Workaround this bug https://github.com/dotnet/aspnetcore/issues/19578 + app.UseStaticFiles(new StaticFileOptions() + { + RequestPath = "/_blazorfiles", + FileProvider = new ManifestEmbeddedFileProvider(typeof(ComponentServiceCollectionExtensions).Assembly), + OnPrepareResponse = LongCache + }); app.UseStaticFiles(new StaticFileOptions { - OnPrepareResponse = ctx => - { - // Cache static assets for one year, set asp-append-version="true" on references to update on change. - // https://andrewlock.net/adding-cache-control-headers-to-static-files-in-asp-net-core/ - const int durationInSeconds = 60 * 60 * 24 * 365; - ctx.Context.Response.Headers[HeaderNames.CacheControl] = "public,max-age=" + durationInSeconds; - } + OnPrepareResponse = LongCache }); // The framework during publish automatically publish the js files into @@ -300,10 +312,12 @@ namespace BTCPayServer.Hosting HttpOnly = Microsoft.AspNetCore.CookiePolicy.HttpOnlyPolicy.Always, Secure = Microsoft.AspNetCore.Http.CookieSecurePolicy.SameAsRequest }); + app.UseEndpoints(endpoints => { AppHub.Register(endpoints); PaymentRequestHub.Register(endpoints); + endpoints.MapBlazorHub().RequireAuthorization(); endpoints.MapRazorPages(); endpoints.MapControllers(); endpoints.MapControllerRoute("default", "{controller:validate=UIHome}/{action:lowercase=Index}/{id?}"); @@ -311,6 +325,14 @@ namespace BTCPayServer.Hosting app.UsePlugins(); } + private static void LongCache(Microsoft.AspNetCore.StaticFiles.StaticFileResponseContext ctx) + { + // Cache static assets for one year, set asp-append-version="true" on references to update on change. + // https://andrewlock.net/adding-cache-control-headers-to-static-files-in-asp-net-core/ + const int durationInSeconds = 60 * 60 * 24 * 365; + ctx.Context.Response.Headers[HeaderNames.CacheControl] = "public,max-age=" + durationInSeconds; + } + private static Action NewMethod() { return ctx => diff --git a/BTCPayServer/Views/Shared/LayoutFoot.cshtml b/BTCPayServer/Views/Shared/LayoutFoot.cshtml index c4e0cc857..fee0f32c0 100644 --- a/BTCPayServer/Views/Shared/LayoutFoot.cshtml +++ b/BTCPayServer/Views/Shared/LayoutFoot.cshtml @@ -1,7 +1,11 @@ - + +@if (User.Identity.IsAuthenticated) +{ + +} diff --git a/BTCPayServer/Views/Shared/_Footer.cshtml b/BTCPayServer/Views/Shared/_Footer.cshtml index 13290c14a..8dcf71ed3 100644 --- a/BTCPayServer/Views/Shared/_Footer.cshtml +++ b/BTCPayServer/Views/Shared/_Footer.cshtml @@ -48,3 +48,19 @@ } +@if (User.Identity.IsAuthenticated) +{ +
+ +
+ +} diff --git a/BTCPayServer/Views/UIReports/StoreReports.cshtml b/BTCPayServer/Views/UIReports/StoreReports.cshtml index 548cc5689..23c0b759c 100644 --- a/BTCPayServer/Views/UIReports/StoreReports.cshtml +++ b/BTCPayServer/Views/UIReports/StoreReports.cshtml @@ -21,7 +21,7 @@