From 46cfd731c698af57b98e22fe5e97aae431915b4c Mon Sep 17 00:00:00 2001 From: danjov Date: Tue, 6 Aug 2024 15:15:00 +0200 Subject: [PATCH 1/6] Rename API documentation title --- src/api/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/Program.cs b/src/api/Program.cs index 808d4c946..b36a372ab 100644 --- a/src/api/Program.cs +++ b/src/api/Program.cs @@ -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 { From bf93b76fabdcc7162061a133f3c6b62a5237a70e Mon Sep 17 00:00:00 2001 From: danjov Date: Tue, 6 Aug 2024 15:15:00 +0200 Subject: [PATCH 2/6] Add db health check --- src/api/BDMS.csproj | 1 + src/api/Program.cs | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/src/api/BDMS.csproj b/src/api/BDMS.csproj index 4f9574205..3f6f80b9c 100644 --- a/src/api/BDMS.csproj +++ b/src/api/BDMS.csproj @@ -25,6 +25,7 @@ + diff --git a/src/api/Program.cs b/src/api/Program.cs index b36a372ab..5e10a9023 100644 --- a/src/api/Program.cs +++ b/src/api/Program.cs @@ -139,6 +139,10 @@ builder.Services.AddScoped(); builder.Services.AddSingleton(TimeProvider.System); +builder.Services + .AddHealthChecks() + .AddDbContextCheck("Database"); + var app = builder.Build(); // Migrate db changes on startup @@ -166,5 +170,6 @@ app.MapControllers(); app.MapReverseProxy(); +app.MapHealthChecks("/health"); app.Run(); From 28ae54a934a6518029455c39b50a5f34f651e8de Mon Sep 17 00:00:00 2001 From: danjov Date: Tue, 6 Aug 2024 15:15:00 +0200 Subject: [PATCH 3/6] Use BdmsContext type name for connection string retrieval --- src/api/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/Program.cs b/src/api/Program.cs index 5e10a9023..5c6fceb46 100644 --- a/src/api/Program.cs +++ b/src/api/Program.cs @@ -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(connectionString, options => { options.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery); From c3f9aca36643b76afdd7bf64a70b4c22d295a295 Mon Sep 17 00:00:00 2001 From: danjov Date: Tue, 6 Aug 2024 15:15:00 +0200 Subject: [PATCH 4/6] Add S3 health check --- src/api/Program.cs | 5 +++-- src/api/S3HealthCheck.cs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 src/api/S3HealthCheck.cs diff --git a/src/api/Program.cs b/src/api/Program.cs index 5c6fceb46..d1071c059 100644 --- a/src/api/Program.cs +++ b/src/api/Program.cs @@ -141,7 +141,8 @@ builder.Services .AddHealthChecks() - .AddDbContextCheck("Database"); + .AddDbContextCheck("Database") + .AddCheck("S3"); var app = builder.Build(); @@ -170,6 +171,6 @@ app.MapControllers(); app.MapReverseProxy(); -app.MapHealthChecks("/health"); +app.MapHealthChecks("/health").AllowAnonymous(); app.Run(); diff --git a/src/api/S3HealthCheck.cs b/src/api/S3HealthCheck.cs new file mode 100644 index 000000000..2c2b06030 --- /dev/null +++ b/src/api/S3HealthCheck.cs @@ -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; + + /// + /// Creates a new instance of . + /// + /// The client. + /// The object. + public S3HealthCheck(IAmazonS3 s3Client, IConfiguration configuration) => this.s3Client = s3Client; + + /// + public async Task 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); + } +} From 87524c824dfb4510b3954650ddb69610fb054998 Mon Sep 17 00:00:00 2001 From: danjov Date: Tue, 6 Aug 2024 15:15:00 +0200 Subject: [PATCH 5/6] Use health endpoint for docker HEALTHCHECK --- src/api/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/Dockerfile b/src/api/Dockerfile index 0269582c1..35edfd5ab 100644 --- a/src/api/Dockerfile +++ b/src/api/Dockerfile @@ -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 # Switch to the non-root user 'app' defined in the base image USER $APP_UID From 1f29f26745631bf6a5a20f9e3b33bd9d906bc318 Mon Sep 17 00:00:00 2001 From: danjov Date: Tue, 6 Aug 2024 15:15:54 +0200 Subject: [PATCH 6/6] Add release notes --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a8acdce4..852a3c8ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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