Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add health check endpoint for the .NET API #1431

Merged
merged 7 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Tooltips to main side navigation.
- Location hash for tabs in borehole detail view.
- Language dropdown in the header.
- Added health check endpoint for the .NET API.

### Changed

Expand Down
1 change: 1 addition & 0 deletions src/api/BDMS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<PackageReference Include="Humanizer" Version="2.14.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite" Version="8.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ ENV LC_ALL=C.UTF-8

COPY --from=build /app/publish .

HEALTHCHECK CMD curl --fail http://localhost:8080/ || exit 1
HEALTHCHECK CMD curl --fail http://localhost:8080/health || exit 1
danjov marked this conversation as resolved.
Show resolved Hide resolved

# Switch to the non-root user 'app' defined in the base image
USER $APP_UID
Expand Down
10 changes: 8 additions & 2 deletions src/api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
builder.Services.AddHttpContextAccessor();
builder.Services.AddHttpClient();

var connectionString = builder.Configuration.GetConnectionString("BdmsContext");
var connectionString = builder.Configuration.GetConnectionString(nameof(BdmsContext));
builder.Services.AddNpgsql<BdmsContext>(connectionString, options =>
{
options.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
Expand All @@ -73,7 +73,7 @@
options.SwaggerDoc("v2", new OpenApiInfo
{
Version = "v2",
Title = "BDMS REST API v2",
Title = "Boreholes REST API v2",
});
options.AddSecurityDefinition("OpenIdConnect", new OpenApiSecurityScheme
{
Expand Down Expand Up @@ -139,6 +139,11 @@
builder.Services.AddScoped<IBoreholeLockService, BoreholeLockService>();
builder.Services.AddSingleton(TimeProvider.System);

builder.Services
.AddHealthChecks()
.AddDbContextCheck<BdmsContext>("Database")
.AddCheck<S3HealthCheck>("S3");

var app = builder.Build();

// Migrate db changes on startup
Expand Down Expand Up @@ -166,5 +171,6 @@

app.MapControllers();
app.MapReverseProxy();
app.MapHealthChecks("/health").AllowAnonymous();

app.Run();
38 changes: 38 additions & 0 deletions src/api/S3HealthCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Amazon.S3;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System.Net;

namespace BDMS;

public class S3HealthCheck : IHealthCheck
{
private readonly IAmazonS3 s3Client;

/// <summary>
/// Creates a new instance of <see cref="S3HealthCheck"/>.
/// </summary>
/// <param name="s3Client">The <see cref="IAmazonS3"/> client.</param>
/// <param name="configuration">The <see cref="IConfiguration"/> object.</param>
public S3HealthCheck(IAmazonS3 s3Client, IConfiguration configuration) => this.s3Client = s3Client;

/// <inheritdoc />
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
var healthCheckResult = HealthCheckResult.Healthy();

try
{
var response = await s3Client.ListBucketsAsync(cancellationToken).ConfigureAwait(false);
if (response.HttpStatusCode != HttpStatusCode.OK)
{
healthCheckResult = HealthCheckResult.Unhealthy();
}
}
catch (Exception)
{
healthCheckResult = HealthCheckResult.Unhealthy();
}

return await Task.FromResult(healthCheckResult).ConfigureAwait(false);
}
}
Loading