From 224a5e5ff526f33627bc6a8c5c00a6bdccf74309 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sun, 14 Nov 2021 22:32:49 +0100 Subject: [PATCH] Add health check --- src/backend/server/src/Todo.Api/Startup.cs | 43 +++++++++++++++++-- .../Todo.Persistence/DependencyInjection.cs | 11 +++-- .../Mongo/MongoDbConnectionHandler.cs | 6 ++- .../Todo.Persistence/Todo.Persistence.csproj | 1 + 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/backend/server/src/Todo.Api/Startup.cs b/src/backend/server/src/Todo.Api/Startup.cs index 37a8ebb..200e6fe 100644 --- a/src/backend/server/src/Todo.Api/Startup.cs +++ b/src/backend/server/src/Todo.Api/Startup.cs @@ -1,5 +1,7 @@ using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -37,7 +39,10 @@ namespace Todo.Api c.SwaggerDoc("v1", new OpenApiInfo { Title = "Todo.Api", Version = "v1" }); }); - services.AddPersistence(Configuration); + services.AddPersistence(Configuration, out var mongoDbOptions); + services + .AddHealthChecks() + .AddMongoDb(MongoDbConnectionHandler.FormatConnectionString(mongoDbOptions)); services.AddSignalR(); } @@ -45,7 +50,7 @@ namespace Todo.Api public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.MigrateMongoDb(); - + if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); @@ -62,7 +67,39 @@ namespace Todo.Api { endpoints.MapControllers(); endpoints.MapHub("/hubs/todo"); + endpoints.MapHealthChecks("/health/live", new HealthCheckOptions() + { + ResponseWriter = async (context, report) => + { + var response = new HealthCheckResponse() + { + Status = report.Status.ToString(), + HealthChecks = report.Entries.Select(x => new IndividualHealthCheckResponse + { + Component = x.Key, + Status = x.Value.Status.ToString(), + Description = x.Value.Description + }), + HealthCheckDuration = report.TotalDuration + }; + await context.Response.WriteAsJsonAsync(response); + } + }); }); } + + private class HealthCheckResponse + { + public string Status { get; set; } + public IEnumerable HealthChecks { get; set; } + public TimeSpan HealthCheckDuration { get; set; } + } + + private class IndividualHealthCheckResponse + { + public string Status { get; set; } + public string Component { get; set; } + public string Description { get; set; } + } } -} +} \ No newline at end of file diff --git a/src/backend/server/src/Todo.Persistence/DependencyInjection.cs b/src/backend/server/src/Todo.Persistence/DependencyInjection.cs index 154cdbf..29aa794 100644 --- a/src/backend/server/src/Todo.Persistence/DependencyInjection.cs +++ b/src/backend/server/src/Todo.Persistence/DependencyInjection.cs @@ -4,6 +4,7 @@ global using System.Linq; global using System.Collections.Generic; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; using Todo.Core.Interfaces.Persistence; using Todo.Persistence.Mongo; using Todo.Persistence.Mongo.Repositories; @@ -12,12 +13,14 @@ namespace Todo.Persistence { public static class DependencyInjection { - public static IServiceCollection AddPersistence(this IServiceCollection services, IConfiguration configuration) + public static IServiceCollection AddPersistence(this IServiceCollection services, IConfiguration configuration, + out MongoDbOptions mongoDbOptions) { - + var exportableMongoDbOptions = new MongoDbOptions(); var options = configuration.GetRequiredSection("MONGODB"); - Console.WriteLine(options.Value); - + options.Bind(exportableMongoDbOptions); + mongoDbOptions = exportableMongoDbOptions; + services .AddOptions() .Bind(options) diff --git a/src/backend/server/src/Todo.Persistence/Mongo/MongoDbConnectionHandler.cs b/src/backend/server/src/Todo.Persistence/Mongo/MongoDbConnectionHandler.cs index 1f39b12..d85f230 100644 --- a/src/backend/server/src/Todo.Persistence/Mongo/MongoDbConnectionHandler.cs +++ b/src/backend/server/src/Todo.Persistence/Mongo/MongoDbConnectionHandler.cs @@ -21,10 +21,12 @@ public class MongoDbConnectionHandler private static IMongoDatabase CreateConnectionFromOptions(MongoDbOptions options) { - var conn = new MongoClient( - $"mongodb://{options.Username}:{options.Password}@{options.Host}:{options.Port}"); + var conn = new MongoClient(FormatConnectionString(options)); var database = conn.GetDatabase(options.Database); return database; } + + public static string FormatConnectionString(MongoDbOptions options) => + $"mongodb://{options.Username}:{options.Password}@{options.Host}:{options.Port}"; } \ No newline at end of file diff --git a/src/backend/server/src/Todo.Persistence/Todo.Persistence.csproj b/src/backend/server/src/Todo.Persistence/Todo.Persistence.csproj index 3b14b3d..358426b 100644 --- a/src/backend/server/src/Todo.Persistence/Todo.Persistence.csproj +++ b/src/backend/server/src/Todo.Persistence/Todo.Persistence.csproj @@ -11,6 +11,7 @@ +