Merge pull request #5561 from duplicati/feature/add-health-checks
Added healt checks to server.
This commit is contained in:
@@ -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<Settings>(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;
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<AssemblyName>Duplicati.CommandLine.ServerUtil.Implementation</AssemblyName>
|
||||
<Copyright>Copyright © 2024 Team Duplicati, MIT license</Copyright>
|
||||
<DefaultNamespace>Duplicati.CommandLine.ServerUtil</DefaultNamespace>
|
||||
<RootNamespace>Duplicati.CommandLine.ServerUtil</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -30,6 +30,7 @@ public static class Program
|
||||
Logout.Create(),
|
||||
Import.Create(),
|
||||
Export.Create(),
|
||||
Health.Create(),
|
||||
};
|
||||
|
||||
rootCmd = SettingsBinder.AddGlobalOptions(rootCmd);
|
||||
|
||||
@@ -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");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user