From da8b01bb982f4a477e4b2a9b8581feac8a3375d4 Mon Sep 17 00:00:00 2001 From: Masu Baumgartner Date: Mon, 6 May 2024 10:24:45 +0200 Subject: [PATCH] Added server kill confirmation prompt. Improved power action handling --- .../Configuration/ServersConfiguration.cs | 7 +++- Moonlight/Features/Servers/ServersFeature.cs | 6 +++ .../Servers/UI/Layouts/UserLayout.razor | 39 +++++++++++++++++-- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/Moonlight/Features/Servers/Configuration/ServersConfiguration.cs b/Moonlight/Features/Servers/Configuration/ServersConfiguration.cs index 9997ecb..cadd5da 100644 --- a/Moonlight/Features/Servers/Configuration/ServersConfiguration.cs +++ b/Moonlight/Features/Servers/Configuration/ServersConfiguration.cs @@ -1,6 +1,11 @@ +using System.ComponentModel; +using Newtonsoft.Json; + namespace Moonlight.Features.Servers.Configuration; public class ServersConfiguration { - + [JsonProperty("DisableServerKillWarning")] + [Description("With this option you can globally disable the confirmation popup shown when killing a server")] + public bool DisableServerKillWarning { get; set; } = false; } \ No newline at end of file diff --git a/Moonlight/Features/Servers/ServersFeature.cs b/Moonlight/Features/Servers/ServersFeature.cs index 26de2f3..acd1445 100644 --- a/Moonlight/Features/Servers/ServersFeature.cs +++ b/Moonlight/Features/Servers/ServersFeature.cs @@ -5,6 +5,7 @@ using Moonlight.Core.Interfaces; using Moonlight.Core.Models.Abstractions.Feature; using Moonlight.Core.Services; using Moonlight.Features.Servers.Actions; +using Moonlight.Features.Servers.Configuration; using Moonlight.Features.Servers.Http.Middleware; using Moonlight.Features.Servers.Implementations.Diagnose; using Moonlight.Features.Servers.Models.Enums; @@ -29,6 +30,11 @@ public class ServersFeature : MoonlightFeature var config = new ConfigService(PathBuilder.File("storage", "configs", "core.json")); context.Builder.Services.AddSingleton(new JwtService(config.Get().Security.Token)); + // + var configService = new ConfigService(PathBuilder.File("storage", "configs", "servers.json")); + context.Builder.Services.AddSingleton(configService); + + // Assets context.AddAsset("Servers", "css/XtermBlazor.css"); context.AddAsset("Servers", "css/apexcharts.css"); diff --git a/Moonlight/Features/Servers/UI/Layouts/UserLayout.razor b/Moonlight/Features/Servers/UI/Layouts/UserLayout.razor index 815e9df..fd9b91a 100644 --- a/Moonlight/Features/Servers/UI/Layouts/UserLayout.razor +++ b/Moonlight/Features/Servers/UI/Layouts/UserLayout.razor @@ -11,10 +11,15 @@ @using Moonlight.Features.Servers.UI.UserViews @using System.Net.Sockets @using System.Net.WebSockets +@using MoonCore.Exceptions +@using Moonlight.Features.Servers.Configuration +@using MoonCore.Services @inject Repository ServerRepository @inject ServerService ServerService @inject ToastService ToastService +@inject AlertService AlertService +@inject ConfigService ConfigService @implements IDisposable @@ -301,11 +306,39 @@ await InstallTerminal.WriteLine(message); } - private async Task Start() => await ServerService.Console.SendAction(Server, PowerAction.Start); + private async Task Start() => await SendSignalHandled(PowerAction.Start); - private async Task Stop() => await ServerService.Console.SendAction(Server, PowerAction.Stop); + private async Task Stop() => await SendSignalHandled(PowerAction.Stop); - private async Task Kill() => await ServerService.Console.SendAction(Server, PowerAction.Kill); + private async Task Kill() + { + if (!ConfigService.Get().DisableServerKillWarning) + { + if (!await AlertService.YesNo("Do you really want to kill the server? This can result in data loss or corrupted server files")) + return; + } + + await SendSignalHandled(PowerAction.Kill); + } + + private async Task SendSignalHandled(PowerAction action) + { + try + { + await ServerService.Console.SendAction(Server, action); + } + catch (DisplayException) + { + throw; + } + catch (Exception e) + { + Logger.Warn($"An error occured while sending power action {action} to server {Server.Id}:"); + Logger.Warn(e); + + await ToastService.Danger("An error occured while sending power action to server. Check the console for more information"); + } + } public async void Dispose() {