Przeglądaj źródła

Added server kill confirmation prompt. Improved power action handling

Masu Baumgartner 1 rok temu
rodzic
commit
da8b01bb98

+ 6 - 1
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;
 }

+ 6 - 0
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<CoreConfiguration>(PathBuilder.File("storage", "configs", "core.json"));
         context.Builder.Services.AddSingleton(new JwtService<ServersJwtType>(config.Get().Security.Token));
         
+        //
+        var configService = new ConfigService<ServersConfiguration>(PathBuilder.File("storage", "configs", "servers.json"));
+        context.Builder.Services.AddSingleton(configService);
+        
+        // Assets
         context.AddAsset("Servers", "css/XtermBlazor.css");
         
         context.AddAsset("Servers", "css/apexcharts.css");

+ 36 - 3
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<Server> ServerRepository
 @inject ServerService ServerService
 @inject ToastService ToastService
+@inject AlertService AlertService
+@inject ConfigService<ServersConfiguration> 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()
     {