From 872ebcd5eb42237349240bcca1e85ef0bb2f1657 Mon Sep 17 00:00:00 2001 From: Stanley Dimant Date: Tue, 30 Apr 2024 11:34:42 +0200 Subject: [PATCH] remove grpc --- .../config/standalone/server-standalone.json | 4 -- .../Controllers/ClientMessageController.cs | 43 +++++++++++++++++ .../MareSynchronosServer.csproj | 2 - .../Services/ClientMessageService.cs | 47 ------------------- .../MareSynchronosServer/Startup.cs | 9 +--- .../Discord/MareModule.cs | 26 ++++------ .../MareSynchronosServices.csproj | 1 - .../MareSynchronosServices/Startup.cs | 20 -------- .../MareSynchronosShared.csproj | 9 +--- .../Protos/mareservices.proto | 23 --------- .../Utils/ClientMessage.cs | 4 ++ .../Utils/ServicesConfiguration.cs | 4 +- 12 files changed, 61 insertions(+), 131 deletions(-) create mode 100644 MareSynchronosServer/MareSynchronosServer/Controllers/ClientMessageController.cs delete mode 100644 MareSynchronosServer/MareSynchronosServer/Services/ClientMessageService.cs delete mode 100644 MareSynchronosServer/MareSynchronosShared/Protos/mareservices.proto create mode 100644 MareSynchronosServer/MareSynchronosShared/Utils/ClientMessage.cs diff --git a/Docker/run/config/standalone/server-standalone.json b/Docker/run/config/standalone/server-standalone.json index 72cc626..d77469e 100644 --- a/Docker/run/config/standalone/server-standalone.json +++ b/Docker/run/config/standalone/server-standalone.json @@ -52,10 +52,6 @@ "Endpoints": { "Http": { "Url": "http://+:6000" - }, - "Grpc": { - "Protocols": "Http2", - "Url": "http://+:6005" } } }, diff --git a/MareSynchronosServer/MareSynchronosServer/Controllers/ClientMessageController.cs b/MareSynchronosServer/MareSynchronosServer/Controllers/ClientMessageController.cs new file mode 100644 index 0000000..92fc9b2 --- /dev/null +++ b/MareSynchronosServer/MareSynchronosServer/Controllers/ClientMessageController.cs @@ -0,0 +1,43 @@ +using MareSynchronos.API.Data.Enum; +using MareSynchronos.API.SignalR; +using MareSynchronosServer.Hubs; +using MareSynchronosShared.Utils; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.SignalR; + +namespace MareSynchronosServer.Controllers; + +[Route("/msgc")] +[Authorize(Policy = "Internal")] +public class ClientMessageController : Controller +{ + private ILogger _logger; + private IHubContext _hubContext; + + public ClientMessageController(ILogger logger, IHubContext hubContext) + { + _logger = logger; + _hubContext = hubContext; + } + + [Route("sendMessage")] + [HttpPost] + public async Task SendMessage(ClientMessage msg) + { + bool hasUid = !string.IsNullOrEmpty(msg.UID); + + if (!hasUid) + { + _logger.LogInformation("Sending Message of severity {severity} to all online users: {message}", msg.Severity, msg.Message); + await _hubContext.Clients.All.Client_ReceiveServerMessage(msg.Severity, msg.Message).ConfigureAwait(false); + } + else + { + _logger.LogInformation("Sending Message of severity {severity} to user {uid}: {message}", msg.Severity, msg.UID, msg.Message); + await _hubContext.Clients.User(msg.UID).Client_ReceiveServerMessage(msg.Severity, msg.Message).ConfigureAwait(false); + } + + return Empty; + } +} diff --git a/MareSynchronosServer/MareSynchronosServer/MareSynchronosServer.csproj b/MareSynchronosServer/MareSynchronosServer/MareSynchronosServer.csproj index c13e964..11ec481 100644 --- a/MareSynchronosServer/MareSynchronosServer/MareSynchronosServer.csproj +++ b/MareSynchronosServer/MareSynchronosServer/MareSynchronosServer.csproj @@ -21,8 +21,6 @@ - - all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/MareSynchronosServer/MareSynchronosServer/Services/ClientMessageService.cs b/MareSynchronosServer/MareSynchronosServer/Services/ClientMessageService.cs deleted file mode 100644 index ca40ec4..0000000 --- a/MareSynchronosServer/MareSynchronosServer/Services/ClientMessageService.cs +++ /dev/null @@ -1,47 +0,0 @@ -using Grpc.Core; -using MareSynchronos.API.Data.Enum; -using MareSynchronos.API.SignalR; -using MareSynchronosServer.Hubs; -using MareSynchronosShared.Protos; -using Microsoft.AspNetCore.SignalR; -using static MareSynchronosShared.Protos.ClientMessageService; - -namespace MareSynchronosServer.Services; - -public class GrpcClientMessageService : ClientMessageServiceBase -{ - private readonly ILogger _logger; - private readonly IHubContext _hubContext; - - public GrpcClientMessageService(ILogger logger, IHubContext hubContext) - { - _logger = logger; - _hubContext = hubContext; - } - - public override async Task SendClientMessage(ClientMessage request, ServerCallContext context) - { - bool hasUid = !string.IsNullOrEmpty(request.Uid); - - var severity = request.Type switch - { - MessageType.Info => MessageSeverity.Information, - MessageType.Warning => MessageSeverity.Warning, - MessageType.Error => MessageSeverity.Error, - _ => MessageSeverity.Information, - }; - - if (!hasUid) - { - _logger.LogInformation("Sending Message of severity {severity} to all online users: {message}", severity, request.Message); - await _hubContext.Clients.All.Client_ReceiveServerMessage(severity, request.Message).ConfigureAwait(false); - } - else - { - _logger.LogInformation("Sending Message of severity {severity} to user {uid}: {message}", severity, request.Uid, request.Message); - await _hubContext.Clients.User(request.Uid).Client_ReceiveServerMessage(severity, request.Message).ConfigureAwait(false); - } - - return new Empty(); - } -} diff --git a/MareSynchronosServer/MareSynchronosServer/Startup.cs b/MareSynchronosServer/MareSynchronosServer/Startup.cs index 0146ed3..bfc0d9f 100644 --- a/MareSynchronosServer/MareSynchronosServer/Startup.cs +++ b/MareSynchronosServer/MareSynchronosServer/Startup.cs @@ -71,7 +71,7 @@ public class Startup a.FeatureProviders.Remove(a.FeatureProviders.OfType().First()); if (mareConfig.GetValue(nameof(ServerConfiguration.MainServerAddress), defaultValue: null) == null) { - a.FeatureProviders.Add(new AllowedControllersFeatureProvider(typeof(MareServerConfigurationController), typeof(MareAuthBaseConfigurationController), typeof(JwtController))); + a.FeatureProviders.Add(new AllowedControllersFeatureProvider(typeof(MareServerConfigurationController), typeof(MareAuthBaseConfigurationController), typeof(JwtController), typeof(ClientMessageController))); } else { @@ -314,8 +314,6 @@ public class Startup { services.AddSingleton, MareConfigurationServiceServer>(); services.AddSingleton, MareConfigurationServiceServer>(); - - services.AddGrpc(); } } @@ -347,11 +345,6 @@ public class Startup options.Transports = HttpTransportType.WebSockets | HttpTransportType.ServerSentEvents | HttpTransportType.LongPolling; }); - if (config.IsMain) - { - endpoints.MapGrpcService().AllowAnonymous(); - } - endpoints.MapHealthChecks("/health").AllowAnonymous(); endpoints.MapControllers(); diff --git a/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs b/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs index d49f9f3..d0a46c0 100644 --- a/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs +++ b/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs @@ -6,9 +6,8 @@ using Prometheus; using MareSynchronosShared.Models; using MareSynchronosShared.Utils; using MareSynchronosShared.Services; -using Grpc.Net.ClientFactory; -using MareSynchronosShared.Protos; using StackExchange.Redis; +using MareSynchronos.API.Data.Enum; namespace MareSynchronosServices.Discord; @@ -17,17 +16,15 @@ public class MareModule : InteractionModuleBase private readonly ILogger _logger; private readonly IServiceProvider _services; private readonly IConfigurationService _mareServicesConfiguration; - private readonly GrpcClientFactory _grpcClientFactory; private readonly IConnectionMultiplexer _connectionMultiplexer; public MareModule(ILogger logger, IServiceProvider services, IConfigurationService mareServicesConfiguration, - GrpcClientFactory grpcClientFactory, IConnectionMultiplexer connectionMultiplexer) + IConnectionMultiplexer connectionMultiplexer) { _logger = logger; _services = services; _mareServicesConfiguration = mareServicesConfiguration; - _grpcClientFactory = grpcClientFactory; _connectionMultiplexer = connectionMultiplexer; } @@ -82,7 +79,7 @@ public class MareModule : InteractionModuleBase [SlashCommand("message", "ADMIN ONLY: sends a message to clients")] public async Task SendMessageToClients([Summary("message", "Message to send")] string message, - [Summary("severity", "Severity of the message")] MareSynchronosShared.Protos.MessageType messageType = MareSynchronosShared.Protos.MessageType.Info, + [Summary("severity", "Severity of the message")] MessageSeverity messageType = MessageSeverity.Information, [Summary("uid", "User ID to the person to send the message to")] string? uid = null) { _logger.LogInformation("SlashCommand:{userId}:{Method}:{message}:{type}:{uid}", Context.Interaction.User.Id, nameof(SendMessageToClients), message, messageType, uid); @@ -104,13 +101,10 @@ public class MareModule : InteractionModuleBase try { - var client = _grpcClientFactory.CreateClient("MessageClient"); - await client.SendClientMessageAsync(new ClientMessage() - { - Message = message, - Type = messageType, - Uid = uid ?? string.Empty - }); + using HttpClient c = new HttpClient(); + await c.PostAsJsonAsync(new Uri(_mareServicesConfiguration.GetValue + (nameof(ServicesConfiguration.MainServerAddress)), "/msgc/sendMessage"), new ClientMessage(messageType, message, uid ?? string.Empty)) + .ConfigureAwait(false); var discordChannelForMessages = _mareServicesConfiguration.GetValueOrDefault(nameof(ServicesConfiguration.DiscordChannelForMessages), null); if (uid == null && discordChannelForMessages != null) @@ -120,9 +114,9 @@ public class MareModule : InteractionModuleBase { var embedColor = messageType switch { - MareSynchronosShared.Protos.MessageType.Info => Color.Blue, - MareSynchronosShared.Protos.MessageType.Warning => new Color(255, 255, 0), - MareSynchronosShared.Protos.MessageType.Error => Color.Red, + MessageSeverity.Information => Color.Blue, + MessageSeverity.Warning => new Color(255, 255, 0), + MessageSeverity.Error => Color.Red, _ => Color.Blue }; diff --git a/MareSynchronosServer/MareSynchronosServices/MareSynchronosServices.csproj b/MareSynchronosServer/MareSynchronosServices/MareSynchronosServices.csproj index 1de88d1..db6d3b3 100644 --- a/MareSynchronosServer/MareSynchronosServices/MareSynchronosServices.csproj +++ b/MareSynchronosServer/MareSynchronosServices/MareSynchronosServices.csproj @@ -22,7 +22,6 @@ - all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/MareSynchronosServer/MareSynchronosServices/Startup.cs b/MareSynchronosServer/MareSynchronosServices/Startup.cs index a6d135c..3128203 100644 --- a/MareSynchronosServer/MareSynchronosServices/Startup.cs +++ b/MareSynchronosServer/MareSynchronosServices/Startup.cs @@ -4,8 +4,6 @@ using MareSynchronosShared.Metrics; using Microsoft.EntityFrameworkCore; using Prometheus; using MareSynchronosShared.Utils; -using Grpc.Net.Client.Configuration; -using MareSynchronosShared.Protos; using MareSynchronosShared.Services; using StackExchange.Redis; using MessagePack.Resolvers; @@ -52,12 +50,6 @@ public class Startup services.AddSingleton(m => new MareMetrics(m.GetService>(), new List { }, new List { })); - var noRetryConfig = new MethodConfig - { - Names = { MethodName.Default }, - RetryPolicy = null - }; - var redis = mareConfig.GetValue(nameof(ServerConfiguration.RedisConnectionString), string.Empty); var options = ConfigurationOptions.Parse(redis); options.ClientName = "Mare"; @@ -65,18 +57,6 @@ public class Startup ConnectionMultiplexer connectionMultiplexer = ConnectionMultiplexer.Connect(options); services.AddSingleton(connectionMultiplexer); - services.AddGrpcClient("MessageClient", c => - { - c.Address = new Uri(mareConfig.GetValue(nameof(ServicesConfiguration.MainServerGrpcAddress))); - }).ConfigureChannel(c => - { - c.ServiceConfig = new ServiceConfig { MethodConfigs = { noRetryConfig } }; - c.HttpHandler = new SocketsHttpHandler() - { - EnableMultipleHttp2Connections = true - }; - }); - var signalRServiceBuilder = services.AddSignalR(hubOptions => { hubOptions.MaximumReceiveMessageSize = long.MaxValue; diff --git a/MareSynchronosServer/MareSynchronosShared/MareSynchronosShared.csproj b/MareSynchronosServer/MareSynchronosShared/MareSynchronosShared.csproj index fa5a231..f1e1092 100644 --- a/MareSynchronosServer/MareSynchronosShared/MareSynchronosShared.csproj +++ b/MareSynchronosServer/MareSynchronosShared/MareSynchronosShared.csproj @@ -5,10 +5,6 @@ enable - - - - @@ -20,7 +16,6 @@ - all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -58,9 +53,7 @@ - - Both - + diff --git a/MareSynchronosServer/MareSynchronosShared/Protos/mareservices.proto b/MareSynchronosServer/MareSynchronosShared/Protos/mareservices.proto deleted file mode 100644 index 8426269..0000000 --- a/MareSynchronosServer/MareSynchronosShared/Protos/mareservices.proto +++ /dev/null @@ -1,23 +0,0 @@ -syntax = "proto3"; - -option csharp_namespace = "MareSynchronosShared.Protos"; - -package mareservices; - -service ClientMessageService { - rpc SendClientMessage (ClientMessage) returns (Empty); -} - -message ClientMessage { - MessageType type = 1; - string message = 2; - string uid = 3; -} - -enum MessageType { - INFO = 0; - WARNING = 1; - ERROR = 2; -} - -message Empty { } \ No newline at end of file diff --git a/MareSynchronosServer/MareSynchronosShared/Utils/ClientMessage.cs b/MareSynchronosServer/MareSynchronosShared/Utils/ClientMessage.cs new file mode 100644 index 0000000..cc7e294 --- /dev/null +++ b/MareSynchronosServer/MareSynchronosShared/Utils/ClientMessage.cs @@ -0,0 +1,4 @@ +using MareSynchronos.API.Data.Enum; + +namespace MareSynchronosShared.Utils; +public record ClientMessage(MessageSeverity Severity, string Message, string UID); diff --git a/MareSynchronosServer/MareSynchronosShared/Utils/ServicesConfiguration.cs b/MareSynchronosServer/MareSynchronosShared/Utils/ServicesConfiguration.cs index 938d6d6..df09f4c 100644 --- a/MareSynchronosServer/MareSynchronosShared/Utils/ServicesConfiguration.cs +++ b/MareSynchronosServer/MareSynchronosShared/Utils/ServicesConfiguration.cs @@ -9,7 +9,7 @@ public class ServicesConfiguration : MareConfigurationBase public ulong? DiscordChannelForReports { get; set; } = null; public ulong? DiscordChannelForCommands { get; set; } = null; public ulong? DiscordRoleAprilFools2024 { get; set; } = null; - public Uri MainServerGrpcAddress { get; set; } = null; + public Uri MainServerAddress { get; set; } = null; public Dictionary VanityRoles { get; set; } = new Dictionary(); public override string ToString() @@ -17,7 +17,7 @@ public class ServicesConfiguration : MareConfigurationBase StringBuilder sb = new(); sb.AppendLine(base.ToString()); sb.AppendLine($"{nameof(DiscordBotToken)} => {DiscordBotToken}"); - sb.AppendLine($"{nameof(MainServerGrpcAddress)} => {MainServerGrpcAddress}"); + sb.AppendLine($"{nameof(MainServerAddress)} => {MainServerAddress}"); sb.AppendLine($"{nameof(DiscordChannelForMessages)} => {DiscordChannelForMessages}"); sb.AppendLine($"{nameof(DiscordChannelForReports)} => {DiscordChannelForReports}"); sb.AppendLine($"{nameof(DiscordChannelForCommands)} => {DiscordChannelForCommands}");