mirror of https://github.com/abpframework/eventhub
11 changed files with 178 additions and 21 deletions
@ -0,0 +1,19 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>net8.0</TargetFramework> |
|||
<ImplicitUsings>enable</ImplicitUsings> |
|||
<Nullable>enable</Nullable> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Volo.Abp.AspNetCore" Version="8.3.1" /> |
|||
<PackageReference Include="AspNetCore.HealthChecks.UI" Version="8.0.2.0" /> |
|||
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1.0" /> |
|||
<PackageReference Include="AspNetCore.HealthChecks.UI.InMemory.Storage" Version="8.0.1.0" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\EventHub.EntityFrameworkCore\EventHub.EntityFrameworkCore.csproj" /> |
|||
</ItemGroup> |
|||
</Project> |
|||
@ -0,0 +1,15 @@ |
|||
using EventHub.EntityFrameworkCore; |
|||
using EventHub.Web.Shared.HealthChecks; |
|||
using Volo.Abp.AspNetCore; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace EventHub.Web.Shared; |
|||
|
|||
[DependsOn(typeof(AbpAspNetCoreModule), typeof(EventHubEntityFrameworkCoreModule))] |
|||
public class EventHubWebSharedModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.AddEventHubHealthChecks(); |
|||
} |
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
using Microsoft.Extensions.Diagnostics.HealthChecks; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Identity; |
|||
using IdentityRole = Microsoft.AspNetCore.Identity.IdentityRole; |
|||
|
|||
namespace EventHub.Web.Shared.HealthChecks; |
|||
|
|||
public class EventHubDatabaseCheck : IHealthCheck, ITransientDependency |
|||
{ |
|||
protected readonly IIdentityRoleRepository IdentityRoleRepository; |
|||
|
|||
public EventHubDatabaseCheck(IIdentityRoleRepository identityRoleRepository) |
|||
{ |
|||
IdentityRoleRepository = identityRoleRepository; |
|||
} |
|||
|
|||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) |
|||
{ |
|||
try |
|||
{ |
|||
await IdentityRoleRepository.GetListAsync(sorting: nameof(IdentityRole.Id), maxResultCount: 1, cancellationToken: cancellationToken); |
|||
|
|||
return HealthCheckResult.Healthy($"Could connect to database and get record."); |
|||
} |
|||
catch (Exception e) |
|||
{ |
|||
return HealthCheckResult.Unhealthy($"Error when trying to get database record. ", e); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,76 @@ |
|||
using HealthChecks.UI.Client; |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.AspNetCore.Diagnostics.HealthChecks; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.AspNetCore.Routing; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace EventHub.Web.Shared.HealthChecks; |
|||
|
|||
public static class HealthChecksBuilderExtensions |
|||
{ |
|||
public static void AddEventHubHealthChecks(this IServiceCollection services) |
|||
{ |
|||
// Add your health checks here
|
|||
var healthChecksBuilder = services.AddHealthChecks(); |
|||
healthChecksBuilder.AddCheck<EventHubDatabaseCheck>("EventHub DbContext Check", tags: new string[] { "database" }); |
|||
|
|||
services.ConfigureHealthCheckEndpoint("/health-status"); |
|||
|
|||
// If you don't want to add HealthChecksUI, remove following configurations.
|
|||
var configuration = services.GetConfiguration(); |
|||
var healthCheckUrl = configuration["App:HealthCheckUrl"]; |
|||
|
|||
if (string.IsNullOrEmpty(healthCheckUrl)) |
|||
{ |
|||
healthCheckUrl = "/health-status"; |
|||
} |
|||
|
|||
var healthChecksUiBuilder = services.AddHealthChecksUI(settings => |
|||
{ |
|||
settings.AddHealthCheckEndpoint("EventHub Health Status", healthCheckUrl); |
|||
}); |
|||
|
|||
// Set your HealthCheck UI Storage here
|
|||
healthChecksUiBuilder.AddInMemoryStorage(); |
|||
|
|||
services.MapHealthChecksUiEndpoints(options => |
|||
{ |
|||
options.UIPath = "/health-ui"; |
|||
options.ApiPath = "/health-api"; |
|||
}); |
|||
} |
|||
|
|||
private static IServiceCollection ConfigureHealthCheckEndpoint(this IServiceCollection services, string path) |
|||
{ |
|||
services.Configure<AbpEndpointRouterOptions>(options => |
|||
{ |
|||
options.EndpointConfigureActions.Add(endpointContext => |
|||
{ |
|||
endpointContext.Endpoints.MapHealthChecks( |
|||
new PathString(path.EnsureStartsWith('/')), |
|||
new HealthCheckOptions |
|||
{ |
|||
Predicate = _ => true, |
|||
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse, |
|||
AllowCachingResponses = false, |
|||
}); |
|||
}); |
|||
}); |
|||
|
|||
return services; |
|||
} |
|||
|
|||
private static IServiceCollection MapHealthChecksUiEndpoints(this IServiceCollection services, Action<global::HealthChecks.UI.Configuration.Options>? setupOption = null) |
|||
{ |
|||
services.Configure<AbpEndpointRouterOptions>(routerOptions => |
|||
{ |
|||
routerOptions.EndpointConfigureActions.Add(endpointContext => |
|||
{ |
|||
endpointContext.Endpoints.MapHealthChecksUI(setupOption); |
|||
}); |
|||
}); |
|||
|
|||
return services; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue