-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #685 from neozhu/removeserverproject
🪶 clean project remove server project
- Loading branch information
Showing
12 changed files
with
265 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/Server.UI/Middlewares/HangfireDashboardAuthorizationFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
src/Server.UI/Middlewares/LocalizationCookiesMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters