From 4fc28378c5960a4a89d5c02d171169b12daad50c Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Fri, 25 Oct 2024 02:36:39 +0300 Subject: [PATCH] feat: add healthcheck for main project --- .../Core/Startup/CacheConfiguration.cs | 18 ++++++++++-------- Endpoint/Endpoint.csproj | 2 ++ Endpoint/Program.cs | 16 +++++++++++++--- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Endpoint/Configuration/Core/Startup/CacheConfiguration.cs b/Endpoint/Configuration/Core/Startup/CacheConfiguration.cs index 504a655..ca3ddb8 100644 --- a/Endpoint/Configuration/Core/Startup/CacheConfiguration.cs +++ b/Endpoint/Configuration/Core/Startup/CacheConfiguration.cs @@ -7,17 +7,19 @@ namespace Mirea.Api.Endpoint.Configuration.Core.Startup; public static class CacheConfiguration { - public static IServiceCollection AddCustomRedis(this IServiceCollection services, IConfiguration configuration) + public static IServiceCollection AddCustomRedis(this IServiceCollection services, IConfiguration configuration, IHealthChecksBuilder? healthChecksBuilder = null) { var cache = configuration.Get()?.CacheSettings; - if (cache?.TypeDatabase == CacheSettings.CacheEnum.Redis) + if (cache?.TypeDatabase != CacheSettings.CacheEnum.Redis) + return services; + + services.AddStackExchangeRedisCache(options => { - services.AddStackExchangeRedisCache(options => - { - options.Configuration = cache.ConnectionString; - options.InstanceName = "mirea_"; - }); - } + options.Configuration = cache.ConnectionString; + options.InstanceName = "mirea_"; + }); + + healthChecksBuilder?.AddRedis(cache.ConnectionString!, name: "Redis"); return services; } diff --git a/Endpoint/Endpoint.csproj b/Endpoint/Endpoint.csproj index c61dd9e..5e86f4e 100644 --- a/Endpoint/Endpoint.csproj +++ b/Endpoint/Endpoint.csproj @@ -24,6 +24,8 @@ + + diff --git a/Endpoint/Program.cs b/Endpoint/Program.cs index 0b39b2a..5d16dc2 100644 --- a/Endpoint/Program.cs +++ b/Endpoint/Program.cs @@ -23,7 +23,7 @@ namespace Mirea.Api.Endpoint; public class Program { - public static IServiceCollection AddDatabase(IServiceCollection services, IConfiguration configuration) + public static IServiceCollection AddDatabase(IServiceCollection services, IConfiguration configuration, IHealthChecksBuilder? healthCheckBuilder = null) { var dbSettings = configuration.Get()?.DbSettings; services.AddApplication(); @@ -31,6 +31,10 @@ public class Program dbSettings?.DatabaseProvider ?? DatabaseProvider.Sqlite, dbSettings?.ConnectionStringSql ?? string.Empty); + healthCheckBuilder?.AddDatabaseHealthCheck( + dbSettings?.DatabaseProvider ?? DatabaseProvider.Sqlite, + dbSettings?.ConnectionStringSql ?? string.Empty); + return services; } @@ -40,13 +44,18 @@ public class Program var builder = WebApplication.CreateBuilder(args); builder.Configuration.AddConfiguration(EnvironmentConfiguration.GetEnvironment()); + + var healthCheckBuilder = builder.Services.AddHealthChecks(); builder.Configuration.AddJsonFile(GeneralConfig.FilePath, optional: true, reloadOnChange: true); builder.Services.Configure(builder.Configuration); + healthCheckBuilder.AddFile(x => x.AddFile(GeneralConfig.FilePath), name: nameof(GeneralConfig)); + builder.Configuration.AddJsonFile(Admin.FilePath, optional: true, reloadOnChange: true); builder.Services.Configure(builder.Configuration); + healthCheckBuilder.AddFile(x => x.AddFile(Admin.FilePath), name: nameof(Admin)); builder.Host.AddCustomSerilog(); - AddDatabase(builder.Services, builder.Configuration); + AddDatabase(builder.Services, builder.Configuration, healthCheckBuilder); builder.Services.AddControllers(); @@ -55,7 +64,7 @@ public class Program builder.Services.AddSingleton(); builder.Services.AddMemoryCache(); - builder.Services.AddCustomRedis(builder.Configuration); + builder.Services.AddCustomRedis(builder.Configuration, healthCheckBuilder); builder.Services.AddCors(options => { @@ -108,6 +117,7 @@ public class Program app.UseStaticFiles(UrlHelper.GetSubPath.TrimEnd('/')); app.UseCors("AllowAll"); app.UseCustomSerilog(); + app.MapHealthChecks("/health"); using (var scope = app.Services.CreateScope()) {