diff --git a/Duplicati/CommandLine/ServerUtil/Commands/Health.cs b/Duplicati/CommandLine/ServerUtil/Commands/Health.cs new file mode 100644 index 000000000..866e697d4 --- /dev/null +++ b/Duplicati/CommandLine/ServerUtil/Commands/Health.cs @@ -0,0 +1,38 @@ +using System.CommandLine; +using System.CommandLine.NamingConventionBinder; + +namespace Duplicati.CommandLine.ServerUtil.Commands; + +public static class Health +{ + public static Command Create() => + new Command("health", "Checks the server health endpoint") + .WithHandler(CommandHandler.Create(async (settings) => + { + using var client = new HttpClient(new HttpClientHandler() + { + ServerCertificateCustomValidationCallback = settings.Insecure + ? HttpClientHandler.DangerousAcceptAnyServerCertificateValidator + : null + }) + { + BaseAddress = settings.HostUrl, + Timeout = TimeSpan.FromSeconds(10) + }; + + try + { + var response = await client.GetAsync("health"); + response.EnsureSuccessStatusCode(); + + Console.WriteLine("Server is healthy"); + return 0; + } + catch (HttpRequestException) + { + Console.WriteLine("Server is unhealthy"); + return 1; + } + }) + ); +} diff --git a/Duplicati/CommandLine/ServerUtil/Duplicati.CommandLine.ServerUtil.csproj b/Duplicati/CommandLine/ServerUtil/Duplicati.CommandLine.ServerUtil.csproj index 499ac9732..cebbdee7a 100644 --- a/Duplicati/CommandLine/ServerUtil/Duplicati.CommandLine.ServerUtil.csproj +++ b/Duplicati/CommandLine/ServerUtil/Duplicati.CommandLine.ServerUtil.csproj @@ -6,7 +6,7 @@ enable Duplicati.CommandLine.ServerUtil.Implementation Copyright © 2024 Team Duplicati, MIT license - Duplicati.CommandLine.ServerUtil + Duplicati.CommandLine.ServerUtil diff --git a/Duplicati/CommandLine/ServerUtil/Program.cs b/Duplicati/CommandLine/ServerUtil/Program.cs index 486790e2d..1e17bb8ab 100644 --- a/Duplicati/CommandLine/ServerUtil/Program.cs +++ b/Duplicati/CommandLine/ServerUtil/Program.cs @@ -30,6 +30,7 @@ public static class Program Logout.Create(), Import.Create(), Export.Create(), + Health.Create(), }; rootCmd = SettingsBinder.AddGlobalOptions(rootCmd); diff --git a/Duplicati/WebserverCore/DuplicatiWebserver.cs b/Duplicati/WebserverCore/DuplicatiWebserver.cs index 0517ebca6..b000baaa3 100644 --- a/Duplicati/WebserverCore/DuplicatiWebserver.cs +++ b/Duplicati/WebserverCore/DuplicatiWebserver.cs @@ -12,6 +12,7 @@ using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Http.Json; using Microsoft.Extensions.Configuration.Json; +using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.OpenApi.Models; namespace Duplicati.WebserverCore; @@ -156,6 +157,9 @@ public partial class DuplicatiWebserver }; }); + builder.Services.AddHealthChecks() + .AddCheck("Basic", () => HealthCheckResult.Healthy("Service is running")); + builder.Services.AddDuplicati(connection); // Prevent logs from spamming the console, but allow enabling for debugging @@ -216,6 +220,7 @@ public partial class DuplicatiWebserver public Task Start(InitSettings settings) { + App.MapHealthChecks("/health"); App.AddEndpoints() .UseNotifications("/notifications");