mirror of
https://github.com/bitwarden/server.git
synced 2025-04-19 03:58:13 -05:00
[PM-18881] /healthz endpoint for Billing
This commit is contained in:
parent
bd7a0a8ed8
commit
ef51de24ad
@ -13,6 +13,7 @@
|
|||||||
<ProjectReference Include="..\Core\Core.csproj" />
|
<ProjectReference Include="..\Core\Core.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="8.0.13" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
25
src/Billing/HealthChecks/StripeHealthCheck.cs
Normal file
25
src/Billing/HealthChecks/StripeHealthCheck.cs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||||
|
using Stripe;
|
||||||
|
|
||||||
|
namespace Bit.Billing.HealthChecks;
|
||||||
|
|
||||||
|
public class StripeHealthCheck : IHealthCheck
|
||||||
|
{
|
||||||
|
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = new())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var accountService = new AccountService();
|
||||||
|
_ = await accountService.ListAsync(
|
||||||
|
new AccountListOptions { Limit = 1 },
|
||||||
|
null,
|
||||||
|
cancellationToken);
|
||||||
|
|
||||||
|
return HealthCheckResult.Healthy();
|
||||||
|
}
|
||||||
|
catch (StripeException)
|
||||||
|
{
|
||||||
|
return HealthCheckResult.Unhealthy("Stripe is down.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
|
using Bit.Billing.HealthChecks;
|
||||||
using Bit.Billing.Services;
|
using Bit.Billing.Services;
|
||||||
using Bit.Billing.Services.Implementations;
|
using Bit.Billing.Services.Implementations;
|
||||||
using Bit.Core.Billing.Extensions;
|
using Bit.Core.Billing.Extensions;
|
||||||
@ -8,7 +9,9 @@ using Bit.Core.SecretsManager.Repositories;
|
|||||||
using Bit.Core.SecretsManager.Repositories.Noop;
|
using Bit.Core.SecretsManager.Repositories.Noop;
|
||||||
using Bit.Core.Settings;
|
using Bit.Core.Settings;
|
||||||
using Bit.Core.Utilities;
|
using Bit.Core.Utilities;
|
||||||
|
using Bit.Infrastructure.EntityFramework.Repositories;
|
||||||
using Bit.SharedWeb.Utilities;
|
using Bit.SharedWeb.Utilities;
|
||||||
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
||||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
using Quartz;
|
using Quartz;
|
||||||
using Stripe;
|
using Stripe;
|
||||||
@ -88,10 +91,7 @@ public class Startup
|
|||||||
services.AddScoped<IServiceAccountRepository, NoopServiceAccountRepository>();
|
services.AddScoped<IServiceAccountRepository, NoopServiceAccountRepository>();
|
||||||
|
|
||||||
// Mvc
|
// Mvc
|
||||||
services.AddMvc(config =>
|
services.AddMvc(config => { config.Filters.Add(new LoggingExceptionHandlerFilterAttribute()); });
|
||||||
{
|
|
||||||
config.Filters.Add(new LoggingExceptionHandlerFilterAttribute());
|
|
||||||
});
|
|
||||||
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
|
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
|
||||||
|
|
||||||
// Authentication
|
// Authentication
|
||||||
@ -99,9 +99,11 @@ public class Startup
|
|||||||
|
|
||||||
// Set up HttpClients
|
// Set up HttpClients
|
||||||
services.AddHttpClient("FreshdeskApi");
|
services.AddHttpClient("FreshdeskApi");
|
||||||
services.AddHttpClient("OnyxApi", client =>
|
services.AddHttpClient("OnyxApi",
|
||||||
|
client =>
|
||||||
{
|
{
|
||||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", billingSettings.Onyx.ApiKey);
|
client.DefaultRequestHeaders.Authorization =
|
||||||
|
new AuthenticationHeaderValue("Bearer", billingSettings.Onyx.ApiKey);
|
||||||
});
|
});
|
||||||
|
|
||||||
services.AddScoped<IStripeFacade, StripeFacade>();
|
services.AddScoped<IStripeFacade, StripeFacade>();
|
||||||
@ -109,10 +111,7 @@ public class Startup
|
|||||||
services.AddScoped<IProviderEventService, ProviderEventService>();
|
services.AddScoped<IProviderEventService, ProviderEventService>();
|
||||||
|
|
||||||
// Add Quartz services first
|
// Add Quartz services first
|
||||||
services.AddQuartz(q =>
|
services.AddQuartz(q => { q.UseMicrosoftDependencyInjectionJobFactory(); });
|
||||||
{
|
|
||||||
q.UseMicrosoftDependencyInjectionJobFactory();
|
|
||||||
});
|
|
||||||
services.AddQuartzHostedService();
|
services.AddQuartzHostedService();
|
||||||
|
|
||||||
// Jobs service
|
// Jobs service
|
||||||
@ -122,6 +121,10 @@ public class Startup
|
|||||||
// Swagger
|
// Swagger
|
||||||
services.AddEndpointsApiExplorer();
|
services.AddEndpointsApiExplorer();
|
||||||
services.AddSwaggerGen();
|
services.AddSwaggerGen();
|
||||||
|
|
||||||
|
services.AddHealthChecks()
|
||||||
|
.AddDbContextCheck<DatabaseContext>("ef", tags: ["db"])
|
||||||
|
.AddCheck<StripeHealthCheck>("stripe", tags: ["ext-api"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Configure(
|
public void Configure(
|
||||||
@ -149,6 +152,18 @@ public class Startup
|
|||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute());
|
app.UseEndpoints(endpoints =>
|
||||||
|
{
|
||||||
|
endpoints.MapDefaultControllerRoute();
|
||||||
|
|
||||||
|
endpoints.MapHealthChecks("/healthz", new HealthCheckOptions
|
||||||
|
{
|
||||||
|
Predicate = registration =>
|
||||||
|
{
|
||||||
|
string[] tags = ["db", "ext-api"];
|
||||||
|
return registration.Tags.Any(tag => tags.Contains(tag));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user