feat: add healthcheck for main project

This commit is contained in:
Polianin Nikita 2024-10-25 02:36:39 +03:00
parent 98ee3c389c
commit 4fc28378c5
3 changed files with 25 additions and 11 deletions

View File

@ -7,17 +7,19 @@ namespace Mirea.Api.Endpoint.Configuration.Core.Startup;
public static class CacheConfiguration 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<GeneralConfig>()?.CacheSettings; var cache = configuration.Get<GeneralConfig>()?.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.Configuration = cache.ConnectionString;
options.InstanceName = "mirea_"; options.InstanceName = "mirea_";
}); });
}
healthChecksBuilder?.AddRedis(cache.ConnectionString!, name: "Redis");
return services; return services;
} }

View File

@ -24,6 +24,8 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Asp.Versioning.Mvc" Version="8.1.0" /> <PackageReference Include="Asp.Versioning.Mvc" Version="8.1.0" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" /> <PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageReference Include="AspNetCore.HealthChecks.Redis" Version="8.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.System" Version="8.0.1" />
<PackageReference Include="Cronos" Version="0.8.4" /> <PackageReference Include="Cronos" Version="0.8.4" />
<PackageReference Include="EPPlus" Version="7.4.1" /> <PackageReference Include="EPPlus" Version="7.4.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10" />

View File

@ -23,7 +23,7 @@ namespace Mirea.Api.Endpoint;
public class Program 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<GeneralConfig>()?.DbSettings; var dbSettings = configuration.Get<GeneralConfig>()?.DbSettings;
services.AddApplication(); services.AddApplication();
@ -31,6 +31,10 @@ public class Program
dbSettings?.DatabaseProvider ?? DatabaseProvider.Sqlite, dbSettings?.DatabaseProvider ?? DatabaseProvider.Sqlite,
dbSettings?.ConnectionStringSql ?? string.Empty); dbSettings?.ConnectionStringSql ?? string.Empty);
healthCheckBuilder?.AddDatabaseHealthCheck(
dbSettings?.DatabaseProvider ?? DatabaseProvider.Sqlite,
dbSettings?.ConnectionStringSql ?? string.Empty);
return services; return services;
} }
@ -40,13 +44,18 @@ public class Program
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddConfiguration(EnvironmentConfiguration.GetEnvironment()); builder.Configuration.AddConfiguration(EnvironmentConfiguration.GetEnvironment());
var healthCheckBuilder = builder.Services.AddHealthChecks();
builder.Configuration.AddJsonFile(GeneralConfig.FilePath, optional: true, reloadOnChange: true); builder.Configuration.AddJsonFile(GeneralConfig.FilePath, optional: true, reloadOnChange: true);
builder.Services.Configure<GeneralConfig>(builder.Configuration); builder.Services.Configure<GeneralConfig>(builder.Configuration);
healthCheckBuilder.AddFile(x => x.AddFile(GeneralConfig.FilePath), name: nameof(GeneralConfig));
builder.Configuration.AddJsonFile(Admin.FilePath, optional: true, reloadOnChange: true); builder.Configuration.AddJsonFile(Admin.FilePath, optional: true, reloadOnChange: true);
builder.Services.Configure<Admin>(builder.Configuration); builder.Services.Configure<Admin>(builder.Configuration);
healthCheckBuilder.AddFile(x => x.AddFile(Admin.FilePath), name: nameof(Admin));
builder.Host.AddCustomSerilog(); builder.Host.AddCustomSerilog();
AddDatabase(builder.Services, builder.Configuration); AddDatabase(builder.Services, builder.Configuration, healthCheckBuilder);
builder.Services.AddControllers(); builder.Services.AddControllers();
@ -55,7 +64,7 @@ public class Program
builder.Services.AddSingleton<ISetupToken, SetupTokenService>(); builder.Services.AddSingleton<ISetupToken, SetupTokenService>();
builder.Services.AddMemoryCache(); builder.Services.AddMemoryCache();
builder.Services.AddCustomRedis(builder.Configuration); builder.Services.AddCustomRedis(builder.Configuration, healthCheckBuilder);
builder.Services.AddCors(options => builder.Services.AddCors(options =>
{ {
@ -108,6 +117,7 @@ public class Program
app.UseStaticFiles(UrlHelper.GetSubPath.TrimEnd('/')); app.UseStaticFiles(UrlHelper.GetSubPath.TrimEnd('/'));
app.UseCors("AllowAll"); app.UseCors("AllowAll");
app.UseCustomSerilog(); app.UseCustomSerilog();
app.MapHealthChecks("/health");
using (var scope = app.Services.CreateScope()) using (var scope = app.Services.CreateScope())
{ {