Add health check
This commit is contained in:
parent
232ac18bf7
commit
224a5e5ff5
@ -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<TodoHub>("/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<IndividualHealthCheckResponse> 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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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<MongoDbOptions>()
|
||||
.Bind(options)
|
||||
|
@ -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}";
|
||||
}
|
@ -11,6 +11,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.13.2" />
|
||||
<PackageReference Include="MongoDB.Driver.Core" Version="2.13.2" />
|
||||
<PackageReference Include="AspNetCore.HealthChecks.MongoDb" Version="5.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
Loading…
Reference in New Issue
Block a user