From 17e7e757105c09e7c4a5d42af4604470394b143c Mon Sep 17 00:00:00 2001 From: Amin Ziagham Date: Wed, 16 Sep 2026 21:16:02 +0200 Subject: [PATCH] Migrate app from Blazor WASM to Blazor Server Replaced WebAssembly-specific code and packages with server-side Blazor components and configuration. Updated App.razor to a full HTML structure with splash screen and theme script; routing now handled by Routes.razor. Modified _Host.cshtml for server-side rendering. Refactored Program.cs to use WebApplicationBuilder and modular extension classes for service and endpoint configuration. Added EndpointConfiguration for flexible HTTP/HTTPS setup. Refactored SynentraApiClient to use IHttpClientFactory. Updated MainLayout.razor and app.css for splash screen handling. Cleaned up _Imports.razor for server-side support. #21 --- src/App.razor | 94 +++++++++++++++++-- src/Console.csproj | 2 - .../ApplicationBuilderExtensions.cs | 18 ++++ src/Extensions/ServiceCollectionExtensions.cs | 45 +++++++++ .../WebApplicationBuilderExtensions.cs | 82 ++++++++++++++++ src/Layout/MainLayout.razor | 1 + .../index.html => Pages/_Host.cshtml} | 38 ++------ src/Program.cs | 78 +++++++++++---- src/Routes.razor | 6 ++ src/Services/SynentraApiClient.cs | 4 +- src/Settings/EndpointConfiguration.cs | 25 +++++ src/_Imports.razor | 2 +- src/wwwroot/css/app.css | 7 ++ 13 files changed, 344 insertions(+), 58 deletions(-) create mode 100644 src/Extensions/ApplicationBuilderExtensions.cs create mode 100644 src/Extensions/ServiceCollectionExtensions.cs create mode 100644 src/Extensions/WebApplicationBuilderExtensions.cs rename src/{wwwroot/index.html => Pages/_Host.cshtml} (58%) create mode 100644 src/Routes.razor create mode 100644 src/Settings/EndpointConfiguration.cs diff --git a/src/App.razor b/src/App.razor index aedf94c..66be981 100644 --- a/src/App.razor +++ b/src/App.razor @@ -1,6 +1,88 @@ - - - - - - + + + + + + + Synentra Console + + + + + + + + + + + + +
+
+ + +
+ SYNENTRA +
+
+ +
+
+
+
+ +
+ + Initializing Console +
+
+ + + +
+ An unhandled error has occurred. + Reload + 🗙 +
+ + + + + diff --git a/src/Console.csproj b/src/Console.csproj index 13f9bd4..19473ec 100644 --- a/src/Console.csproj +++ b/src/Console.csproj @@ -22,8 +22,6 @@ - - diff --git a/src/Extensions/ApplicationBuilderExtensions.cs b/src/Extensions/ApplicationBuilderExtensions.cs new file mode 100644 index 0000000..1cd3dd5 --- /dev/null +++ b/src/Extensions/ApplicationBuilderExtensions.cs @@ -0,0 +1,18 @@ +using Console.Settings; + +namespace Console.Extensions; + +public static class ApplicationBuilderExtensions +{ + public static IApplicationBuilder UseHttps(this IApplicationBuilder app) + { + var serviceProvider = app.ApplicationServices; + var endpointConfiguration = serviceProvider.GetService(); + if (endpointConfiguration != null && endpointConfiguration.Https?.Enabled == true) + { + app.UseHttpsRedirection(); + } + + return app; + } +} \ No newline at end of file diff --git a/src/Extensions/ServiceCollectionExtensions.cs b/src/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..64ab9a1 --- /dev/null +++ b/src/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,45 @@ +using Blazored.LocalStorage; +using Console.Preferences; +using Console.Services; +using Console.Settings; +using MudBlazor; +using MudBlazor.Services; + +namespace Console.Extensions; + +public static class ServiceCollectionExtensions +{ + public static IServiceCollection AddConsoleServices(this IServiceCollection services) + { + services.AddServerSideBlazor(); + services + .AddBlazoredLocalStorage() + .AddScoped() + .AddScoped() + .AddScoped() + .AddHttpClient() + .AddHttpContextAccessor(); + + return services; + } + + public static IServiceCollection AddMudBlazor(this IServiceCollection services) => + services.AddMudServices(ConfigureMudSnackbar); + + #region Private Helpers + + private static void ConfigureMudSnackbar(MudServicesConfiguration config) + { + var snackbar = config.SnackbarConfiguration; + snackbar.PositionClass = Defaults.Classes.Position.BottomRight; + snackbar.HideTransitionDuration = 500; + snackbar.ShowTransitionDuration = 500; + snackbar.VisibleStateDuration = 10000; + snackbar.PreventDuplicates = false; + snackbar.NewestOnTop = false; + snackbar.ShowCloseIcon = true; + snackbar.SnackbarVariant = Variant.Filled; + } + + #endregion +} \ No newline at end of file diff --git a/src/Extensions/WebApplicationBuilderExtensions.cs b/src/Extensions/WebApplicationBuilderExtensions.cs new file mode 100644 index 0000000..2f7a736 --- /dev/null +++ b/src/Extensions/WebApplicationBuilderExtensions.cs @@ -0,0 +1,82 @@ +using Console.Settings; + +namespace Console.Extensions; + +public static class WebApplicationBuilderExtensions +{ + public static WebApplicationBuilder ConfigureHttpServer(this WebApplicationBuilder builder) + { + var endpointConfiguration = new EndpointConfiguration(); + builder.Configuration.GetSection("Endpoints").Bind(endpointConfiguration); + builder.Services.AddSingleton(endpointConfiguration); + + using var scope = builder.Services.BuildServiceProvider().CreateScope(); + var logger = scope.ServiceProvider.GetRequiredService>(); + + ConfigureKestrelEndpoints(builder, endpointConfiguration, logger); + ConfigureKestrelSettings(builder); + + return builder; + } + + private static void ConfigureKestrelEndpoints( + WebApplicationBuilder builder, + EndpointConfiguration config, + ILogger logger) + { + builder.WebHost.ConfigureKestrel((_, options) => + { + var httpPort = config.Http?.Port ?? 8180; + int? httpsPort = null; + + if (config.Https?.Enabled == true) + { + httpsPort = config.Https.Port ?? 8443; + + if (httpsPort == httpPort) + throw new InvalidOperationException($"HTTP and HTTPS endpoint ports cannot be the same: {httpPort}"); + } + + options.ListenAnyIP(httpPort); + + if (httpsPort.HasValue) + { + options.ListenAnyIP(httpsPort.Value, listenOptions => + { + var cert = config.Https!.Certificate; + + if (cert != null) + { + if (!string.IsNullOrWhiteSpace(cert.Password)) + { + listenOptions.UseHttps(cert.Path, cert.Password); + } + else + { + listenOptions.UseHttps(cert.Path); + } + } + else + { + listenOptions.UseHttps(); + } + }); + + logger.LogInformation("Configuring HTTP and HTTPS endpoints: HTTP {HttpPort}, HTTPS {HttpsPort}", httpPort, httpsPort); + } + else + { + logger.LogInformation("Configuring HTTP endpoint only: HTTP {HttpPort}", httpPort); + } + }); + } + + private static void ConfigureKestrelSettings(WebApplicationBuilder builder) + { + builder.WebHost.UseKestrel(options => + { + options.AddServerHeader = false; + options.Limits.MaxRequestBufferSize = null; + }); + } +} \ No newline at end of file diff --git a/src/Layout/MainLayout.razor b/src/Layout/MainLayout.razor index c95934a..2dff7fd 100644 --- a/src/Layout/MainLayout.razor +++ b/src/Layout/MainLayout.razor @@ -60,6 +60,7 @@ _isDarkMode = await PreferenceManager.IsDarkTheme(); _drawerOpen = await PreferenceManager.IsDrawerOpenAsync(); _initialized = true; + await JS.InvokeVoidAsync("synentraLoader.dismiss"); StateHasChanged(); } } diff --git a/src/wwwroot/index.html b/src/Pages/_Host.cshtml similarity index 58% rename from src/wwwroot/index.html rename to src/Pages/_Host.cshtml index 253ccf6..963183e 100644 --- a/src/wwwroot/index.html +++ b/src/Pages/_Host.cshtml @@ -1,3 +1,8 @@ +@page "/" +@namespace Console.Pages +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers +@using Microsoft.AspNetCore.Components.Web + @@ -5,8 +10,7 @@ Synentra Console - - + @@ -31,35 +35,11 @@ document.documentElement.classList.add(themeClass); })(); - + -
-
-
- - -
- SYNENTRA -
-
- -
-
-
-
- -
- - Initializing Console -
-
-
+
An unhandled error has occurred. @@ -67,7 +47,7 @@ 🗙
- + diff --git a/src/Program.cs b/src/Program.cs index 5e87d16..f06f425 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -1,20 +1,60 @@ -using Blazored.LocalStorage; using Console; -using Console.Preferences; -using Console.Services; -using Microsoft.AspNetCore.Components.Web; -using Microsoft.AspNetCore.Components.WebAssembly.Hosting; -using MudBlazor.Services; - -var builder = WebAssemblyHostBuilder.CreateDefault(args); -builder.RootComponents.Add("#app"); -builder.RootComponents.Add("head::after"); - -builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); -builder.Services.AddMudServices(); -builder.Services.AddBlazoredLocalStorage(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); - -await builder.Build().RunAsync(); +using Console.Extensions; + +var builder = WebApplication.CreateBuilder(args); + +builder.Configuration + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile( + "appsettings.json", + optional: true, + reloadOnChange: true) + .AddJsonFile( + $"appsettings.{builder.Environment.EnvironmentName}.json", + optional: true, + reloadOnChange: true) + .AddUserSecrets(optional: true) + .AddEnvironmentVariables() + .AddCommandLine(args); + +var customConfigPath = builder.Configuration["config"]; + +if (!string.IsNullOrEmpty(customConfigPath)) +{ + builder.Configuration.Sources.Clear(); + + builder.Configuration.AddJsonFile( + customConfigPath, + optional: false, + reloadOnChange: false); +} + +builder.ConfigureHttpServer(); + +builder.Services + .AddConsoleServices() + .AddMudBlazor(); + +builder.Services + .AddRazorComponents() + .AddInteractiveServerComponents(); + +var app = builder.Build(); + +if (!app.Environment.IsDevelopment()) +{ + app.UseExceptionHandler("/Error", createScopeForErrors: true); + app.UseHsts(); +} + +app.UseHttps(); + +app.UseAntiforgery(); + +app.UseStaticFiles(); +app.MapStaticAssets(); + +app.MapRazorComponents() + .AddInteractiveServerRenderMode(); + +app.Run(); \ No newline at end of file diff --git a/src/Routes.razor b/src/Routes.razor new file mode 100644 index 0000000..386225d --- /dev/null +++ b/src/Routes.razor @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/Services/SynentraApiClient.cs b/src/Services/SynentraApiClient.cs index f32c77c..f3dc80a 100644 --- a/src/Services/SynentraApiClient.cs +++ b/src/Services/SynentraApiClient.cs @@ -5,8 +5,10 @@ namespace Console.Services; -public sealed class SynentraApiClient(HttpClient httpClient, ConsoleState state) +public sealed class SynentraApiClient(IHttpClientFactory httpClientFactory, ConsoleState state) { + private readonly HttpClient httpClient = httpClientFactory.CreateClient(); + private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true diff --git a/src/Settings/EndpointConfiguration.cs b/src/Settings/EndpointConfiguration.cs new file mode 100644 index 0000000..c467d88 --- /dev/null +++ b/src/Settings/EndpointConfiguration.cs @@ -0,0 +1,25 @@ +namespace Console.Settings; + +public class EndpointConfiguration +{ + public HttpEndpointConfiguration? Http { get; set; } + public HttpsEndpointConfiguration? Https { get; set; } +} + +public class HttpEndpointConfiguration +{ + public int? Port { get; set; } = 8180; +} + +public class HttpsEndpointConfiguration +{ + public bool? Enabled { get; set; } = false; + public int? Port { get; set; } = 8443; + public CertificateOptions? Certificate { get; set; } = new(); +} + +public class CertificateOptions +{ + public string Path { get; set; } = string.Empty; + public string? Password { get; set; } = string.Empty; +} \ No newline at end of file diff --git a/src/_Imports.razor b/src/_Imports.razor index b393e26..d956fb9 100644 --- a/src/_Imports.razor +++ b/src/_Imports.razor @@ -4,7 +4,7 @@ @using Microsoft.AspNetCore.Components.Routing @using Microsoft.AspNetCore.Components.Web @using Microsoft.AspNetCore.Components.Web.Virtualization -@using Microsoft.AspNetCore.Components.WebAssembly.Http +@using static Microsoft.AspNetCore.Components.Web.RenderMode @using Microsoft.JSInterop @using MudBlazor @using Console diff --git a/src/wwwroot/css/app.css b/src/wwwroot/css/app.css index bfa85e5..b2c279c 100644 --- a/src/wwwroot/css/app.css +++ b/src/wwwroot/css/app.css @@ -52,6 +52,13 @@ html, body { animation: loading-fade-in 0.35s ease-out; } +.app-loading-hidden { + opacity: 0; + visibility: hidden; + transition: opacity 0.25s ease, visibility 0.25s ease; + pointer-events: none; +} + /* --------------------------------------------------------- Brand --------------------------------------------------------- */