Skip to content

Commit

Permalink
Merge pull request #685 from neozhu/removeserverproject
Browse files Browse the repository at this point in the history
🪶 clean project remove server project
  • Loading branch information
neozhu committed May 12, 2024
2 parents 9b06aca + 3ad628c commit 9f59364
Show file tree
Hide file tree
Showing 12 changed files with 265 additions and 16 deletions.
16 changes: 9 additions & 7 deletions CleanArchitecture.Blazor.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.32112.339
Expand Down Expand Up @@ -40,8 +39,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Migrators.SqLite", "src\Mig
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server.UI", "src\Server.UI\Server.UI.csproj", "{3E4AF4E5-02EE-4F26-9B6D-8723670172B3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server", "src\Server\Server.csproj", "{E5008BA6-6358-413C-A082-AF14115EF18B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -92,10 +89,6 @@ Global
{3E4AF4E5-02EE-4F26-9B6D-8723670172B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E4AF4E5-02EE-4F26-9B6D-8723670172B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E4AF4E5-02EE-4F26-9B6D-8723670172B3}.Release|Any CPU.Build.0 = Release|Any CPU
{E5008BA6-6358-413C-A082-AF14115EF18B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E5008BA6-6358-413C-A082-AF14115EF18B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5008BA6-6358-413C-A082-AF14115EF18B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E5008BA6-6358-413C-A082-AF14115EF18B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -112,6 +105,15 @@ Global
{08778353-0814-41BB-B49E-36BBA8D7751D} = {5C45543E-B3FA-4C1F-9D7D-DA2E6654BD61}
{469D68F1-379E-46A6-AAC3-CD1E4C0E7D77} = {5C45543E-B3FA-4C1F-9D7D-DA2E6654BD61}
{3E4AF4E5-02EE-4F26-9B6D-8723670172B3} = {21DC0B96-ED87-4130-BA8D-E8CF73903344}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {384CDE06-46A5-4FC5-AED4-7B23339EA56F}
EndGlobalSection
EndGlobal
9-CC8A-4E5B-8894-B00515C90466} = {5C45543E-B3FA-4C1F-9D7D-DA2E6654BD61}
{08778353-0814-41BB-B49E-36BBA8D7751D} = {5C45543E-B3FA-4C1F-9D7D-DA2E6654BD61}
{469D68F1-379E-46A6-AAC3-CD1E4C0E7D77} = {5C45543E-B3FA-4C1F-9D7D-DA2E6654BD61}
{3E4AF4E5-02EE-4F26-9B6D-8723670172B3} = {21DC0B96-ED87-4130-BA8D-E8CF73903344}
{E5008BA6-6358-413C-A082-AF14115EF18B} = {21DC0B96-ED87-4130-BA8D-E8CF73903344}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Expand Down
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ COPY ["src/Migrators/Migrators.PostgreSQL/Migrators.PostgreSQL.csproj", "src/Mig
COPY ["src/Migrators/Migrators.SqLite/Migrators.SqLite.csproj", "src/Migrators/Migrators.SqLite/"]

COPY ["src/Server.UI/Server.UI.csproj", "src/Server.UI/"]
COPY ["src/Server/Server.csproj", "src/Server/"]
COPY ["src/Application/Application.csproj", "src/Application/"]
COPY ["src/Domain/Domain.csproj", "src/Domain/"]
COPY ["src/Infrastructure/Infrastructure.csproj", "src/Infrastructure/"]
Expand Down
31 changes: 27 additions & 4 deletions src/Server.UI/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using BlazorDownloadFile;
using CleanArchitecture.Blazor.Domain.Identity;
using CleanArchitecture.Blazor.Infrastructure.Constants.Localization;
using CleanArchitecture.Blazor.Server.Hubs;
using CleanArchitecture.Blazor.Server.Middlewares;
using CleanArchitecture.Blazor.Server.UI.Hubs;
using CleanArchitecture.Blazor.Server.UI.Services;
using CleanArchitecture.Blazor.Server.UI.Services.Fusion;
Expand All @@ -26,6 +24,7 @@
using ActualLab.Fusion;
using Toolbelt.Blazor.Extensions.DependencyInjection;
using ActualLab.Fusion.Extensions;
using CleanArchitecture.Blazor.Server.UI.Middlewares;

namespace CleanArchitecture.Blazor.Server.UI;

Expand Down Expand Up @@ -63,8 +62,32 @@ public static IServiceCollection AddServerUI(this IServiceCollection services, I
fusion.AddService<IUserSessionTracker,UserSessionTracker>();
fusion.AddService<IOnlineUserTracker, OnlineUserTracker>();
});




services.AddScoped<LocalizationCookiesMiddleware>()
.Configure<RequestLocalizationOptions>(options =>
{
options.AddSupportedUICultures(LocalizationConstants.SupportedLanguages.Select(x => x.Code).ToArray());
options.AddSupportedCultures(LocalizationConstants.SupportedLanguages.Select(x => x.Code).ToArray());
options.FallBackToParentUICultures = true;
})
.AddLocalization(options => options.ResourcesPath = LocalizationConstants.ResourcesPath);

services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseInMemoryStorage())
.AddHangfireServer()
.AddMvc();

services.AddControllers();

services.AddScoped<IApplicationHubWrapper, ServerHubWrapper>()
.AddSignalR();
services.AddExceptionHandler<GlobalExceptionHandler>();
services.AddProblemDetails();
services.AddHealthChecks();


services.AddHttpClient("ocr", c =>
Expand Down
16 changes: 16 additions & 0 deletions src/Server.UI/Hubs/ISignalRHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace CleanArchitecture.Blazor.Server.UI.Hubs;

public interface ISignalRHub
{
public const string Url = "/signalRHub";

Task Connect(string connectionId, string userName);
Task Disconnect(string connectionId, string userName);

Task Start(string message);
Task Completed(string message);

Task SendMessage(string from, string message);
Task SendPrivateMessage(string from, string to, string message);
Task SendNotification(string message);
}
65 changes: 65 additions & 0 deletions src/Server.UI/Hubs/ServerHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Concurrent;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;

namespace CleanArchitecture.Blazor.Server.UI.Hubs;

[Authorize(AuthenticationSchemes = "Identity.Application")]
public class ServerHub : Hub<ISignalRHub>
{
private static readonly ConcurrentDictionary<string, string> OnlineUsers = new();
public override async Task OnConnectedAsync()
{
var connectionId = Context.ConnectionId;
var username = Context.User?.GetDisplayName() ??(Context.User?.Identity?.Name ?? string.Empty);
// Notify all clients if this is a new user connecting.
if (!OnlineUsers.Any(x => x.Value == username))
{
await Clients.All.Connect(connectionId, username);
}
if (!OnlineUsers.ContainsKey(connectionId))
{
OnlineUsers.TryAdd(connectionId, username);
}
await base.OnConnectedAsync();
}

public override async Task OnDisconnectedAsync(Exception? exception)
{
var connectionId = Context.ConnectionId;
// Remove the connection and check if it was the last one for this user.
if (OnlineUsers.TryRemove(connectionId, out var username))
{
if (!OnlineUsers.Any(x => x.Value == username))
{
await Clients.All.Disconnect(connectionId, username);
}
}
await base.OnConnectedAsync();
}

public async Task SendMessage(string message)
{
var username = Context.User?.Identity?.Name ?? string.Empty;
await Clients.All.SendMessage(username, message);
}

public async Task SendPrivateMessage(string to, string message)
{
var username = Context.User?.Identity?.Name ?? string.Empty;
await Clients.User(to).SendPrivateMessage(username, to, message);
}

public async Task SendNotification(string message)
{
await Clients.All.SendNotification(message);
}

public async Task Completed(string message)
{
await Clients.All.Completed(message);
}
}
23 changes: 23 additions & 0 deletions src/Server.UI/Hubs/ServerHubWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.AspNetCore.SignalR;

namespace CleanArchitecture.Blazor.Server.UI.Hubs;

public class ServerHubWrapper : IApplicationHubWrapper
{
private readonly IHubContext<ServerHub, ISignalRHub> _hubContext;

public ServerHubWrapper(IHubContext<ServerHub, ISignalRHub> hubContext)
{
_hubContext = hubContext;
}

public async Task JobStarted(string message)
{
await _hubContext.Clients.All.Start(message);
}

public async Task JobCompleted(string message)
{
await _hubContext.Clients.All.Completed(message);
}
}
51 changes: 51 additions & 0 deletions src/Server.UI/Middlewares/GlobalExceptionHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Diagnostics;
using CleanArchitecture.Blazor.Application.Common.ExceptionHandlers;
using Microsoft.AspNetCore.Diagnostics;

namespace CleanArchitecture.Blazor.Server.UI.Middlewares;

internal sealed class GlobalExceptionHandler(ILogger<GlobalExceptionHandler> logger) : IExceptionHandler
{
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception,
CancellationToken cancellationToken)
{
var traceId = Activity.Current?.Id ?? httpContext.TraceIdentifier;

logger.LogError(exception,
$"Could not process a request on machine {Environment.MachineName}. TraceId: {traceId}");

await GenerateProblemDetails(httpContext, traceId, exception);

return true;
}

private static async Task GenerateProblemDetails(HttpContext httpContext,
string traceId,
Exception exception)
{
var (statusCode, title) = MapExceptionWithStatusCode(exception);

await Results.Problem(title: title,
statusCode: statusCode,
extensions: new Dictionary<string, object?>
{
{
"traceId", traceId
}
}).ExecuteAsync(httpContext);
}

private static (int statusCode, string title) MapExceptionWithStatusCode(Exception exception)
{
if (exception is not ServerException && exception.InnerException != null)
while (exception.InnerException != null)
exception = exception.InnerException;
return exception switch
{
ArgumentOutOfRangeException => (StatusCodes.Status400BadRequest, exception.Message),
ServerException => (StatusCodes.Status500InternalServerError, exception.Message),
KeyNotFoundException => (StatusCodes.Status404NotFound, exception.Message),
_ => (StatusCodes.Status500InternalServerError, "We are sorry for the inconvenience but we are on it.")
};
}
}
19 changes: 19 additions & 0 deletions src/Server.UI/Middlewares/HangfireDashboardAuthorizationFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Hangfire.Dashboard;

namespace CleanArchitecture.Blazor.Server.UI.Middlewares;

public class HangfireDashboardAsyncAuthorizationFilter : IDashboardAsyncAuthorizationFilter
{
public Task<bool> AuthorizeAsync(DashboardContext context)
{
return Task.FromResult(true);
}
}

public class HangfireDashboardAuthorizationFilter : IDashboardAuthorizationFilter
{
public bool Authorize(DashboardContext context)
{
return true;
}
}
41 changes: 41 additions & 0 deletions src/Server.UI/Middlewares/LocalizationCookiesMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Options;
using IMiddleware = Microsoft.AspNetCore.Http.IMiddleware;

namespace CleanArchitecture.Blazor.Server.UI.Middlewares;
#nullable disable
public class LocalizationCookiesMiddleware : IMiddleware
{
public LocalizationCookiesMiddleware(IOptions<RequestLocalizationOptions> requestLocalizationOptions)
{
Provider =
requestLocalizationOptions
.Value
.RequestCultureProviders
.Where(x => x is CookieRequestCultureProvider)
.Cast<CookieRequestCultureProvider>()
.FirstOrDefault();
}

public CookieRequestCultureProvider Provider { get; }

public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
if (Provider != null)
{
var feature = context.Features.Get<IRequestCultureFeature>();

if (feature != null)
// remember culture across request
context.Response
.Cookies
.Append(
Provider.CookieName,
CookieRequestCultureProvider.MakeCookieValue(feature.RequestCulture),
new CookieOptions { Expires = new DateTimeOffset(DateTime.Now.AddMonths(3)) }
);
}

await next(context);
}
}
2 changes: 0 additions & 2 deletions src/Server.UI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using CleanArchitecture.Blazor.Application;
using CleanArchitecture.Blazor.Infrastructure;
using CleanArchitecture.Blazor.Infrastructure.Persistence;
using CleanArchitecture.Blazor.Server;
using CleanArchitecture.Blazor.Server.UI;

var builder = WebApplication.CreateBuilder(args);
Expand All @@ -12,7 +11,6 @@
builder.Services
.AddApplication()
.AddInfrastructure(builder.Configuration)
.AddServer(builder.Configuration)
.AddServerUI(builder.Configuration);

var app = builder.Build();
Expand Down
15 changes: 14 additions & 1 deletion src/Server.UI/Server.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,21 @@
<LangVersion>default</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Fluxor.Blazor.Web" Version="5.9.1" />
<PackageReference Include="Fluxor.Blazor.Web.ReduxDevTools" Version="5.9.1" />
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.12" />
<PackageReference Include="Hangfire.InMemory" Version="0.8.1" />
<PackageReference Include="Blazor-Analytics" Version="3.12.0" />

<PackageReference Include="BlazorDownloadFile" Version="2.4.0.2" />
<PackageReference Include="BlazorTime" Version="1.0.3" />
<PackageReference Include="CodeBeam.MudBlazor.Extensions" Version="6.9.2" />
<PackageReference Include="MudBlazor" Version="6.19.1" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.4" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.4" />
<PackageReference Include="ActualLab.Fusion.Ext.Services" Version="7.9.10" />
<PackageReference Include="CodeBeam.MudBlazor.Extensions" Version="6.9.2" />
<PackageReference Include="MudBlazor" Version="6.19.1" />
<PackageReference Include="Toolbelt.Blazor.HotKeys2" Version="4.1.0.1" />
<PackageReference Include="Blazor-ApexCharts" Version="3.3.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.4">
Expand All @@ -36,7 +50,6 @@
<ProjectReference Include="..\Migrators\Migrators.MSSQL\Migrators.MSSQL.csproj" />
<ProjectReference Include="..\Migrators\Migrators.PostgreSQL\Migrators.PostgreSQL.csproj" />
<ProjectReference Include="..\Migrators\Migrators.SqLite\Migrators.SqLite.csproj" />
<ProjectReference Include="..\Server\Server.csproj" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\EmailTemplates\_useractivation.cshtml">
Expand Down
1 change: 0 additions & 1 deletion src/Server.UI/_Imports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
global using CleanArchitecture.Blazor.Application.Common.Models;
global using CleanArchitecture.Blazor.Infrastructure.Extensions;
global using CleanArchitecture.Blazor.Domain.Entities;
global using CleanArchitecture.Blazor.Server.Common.Interfaces;
global using CleanArchitecture.Blazor.Server.UI.Components;
global using FluentValidation;
global using Fluxor;
Expand Down

0 comments on commit 9f59364

Please sign in to comment.