mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
Added LDAP health checks
This commit is contained in:
31
src/HealthChecks/LDAPHealthChecks.cs
Normal file
31
src/HealthChecks/LDAPHealthChecks.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using Berufsschule_HAM.Services;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
namespace Berufsschule_HAM.HealthChecks;
|
||||
|
||||
public class DatabaseHealthCheck : IHealthCheck
|
||||
{
|
||||
private readonly ILogger<DatabaseHealthCheck> _logger;
|
||||
private readonly LdapService _ldapService;
|
||||
|
||||
public DatabaseHealthCheck(ILogger<DatabaseHealthCheck> logger, LdapService ldapService)
|
||||
{
|
||||
_logger = logger;
|
||||
_ldapService = ldapService;
|
||||
}
|
||||
|
||||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_logger.LogTrace("CheckHealthAsync - Checking health");
|
||||
try
|
||||
{
|
||||
await _ldapService.ListUsersAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "CheckHealthAsync - Unable to list users due to exception {ex.Message} - {ex.StackTrace}", [ex.Message, ex.StackTrace]);
|
||||
return HealthCheckResult.Unhealthy();
|
||||
}
|
||||
_logger.LogTrace("CheckHealthAsync - Health check successful");
|
||||
return HealthCheckResult.Healthy();
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using Serilog;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Berufsschule_HAM.Services;
|
||||
using Berufsschule_HAM.Models;
|
||||
using Berufsschule_HAM.HealthChecks;
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Bind options
|
||||
@@ -26,6 +27,9 @@ builder.Services.AddElmah<XmlFileErrorLog>(Options =>
|
||||
builder.Services.AddSingleton<LdapService>();
|
||||
builder.Services.AddSingleton<MigrationService>();
|
||||
|
||||
builder.Services.AddHealthChecks()
|
||||
.AddCheck<DatabaseHealthCheck>("DatabaseHealthCheck");
|
||||
|
||||
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
||||
.AddCookie(options =>
|
||||
{
|
||||
@@ -53,6 +57,8 @@ app.MapControllerRoute(
|
||||
name: "default",
|
||||
pattern: "{controller=Home}/{action=Index}/{id?}");
|
||||
|
||||
app.MapHealthChecks("/healthz");
|
||||
|
||||
// Run migrations
|
||||
using var scope = app.Services.CreateScope();
|
||||
var migrationService = scope.ServiceProvider.GetRequiredService<MigrationService>();
|
||||
|
||||
Reference in New Issue
Block a user