Add health check
This commit is contained in:
parent
232ac18bf7
commit
224a5e5ff5
@ -1,5 +1,7 @@
|
|||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
@ -37,7 +39,10 @@ namespace Todo.Api
|
|||||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Todo.Api", Version = "v1" });
|
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();
|
services.AddSignalR();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +67,39 @@ namespace Todo.Api
|
|||||||
{
|
{
|
||||||
endpoints.MapControllers();
|
endpoints.MapControllers();
|
||||||
endpoints.MapHub<TodoHub>("/hubs/todo");
|
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;
|
global using System.Collections.Generic;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||||
using Todo.Core.Interfaces.Persistence;
|
using Todo.Core.Interfaces.Persistence;
|
||||||
using Todo.Persistence.Mongo;
|
using Todo.Persistence.Mongo;
|
||||||
using Todo.Persistence.Mongo.Repositories;
|
using Todo.Persistence.Mongo.Repositories;
|
||||||
@ -12,11 +13,13 @@ namespace Todo.Persistence
|
|||||||
{
|
{
|
||||||
public static class DependencyInjection
|
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");
|
var options = configuration.GetRequiredSection("MONGODB");
|
||||||
Console.WriteLine(options.Value);
|
options.Bind(exportableMongoDbOptions);
|
||||||
|
mongoDbOptions = exportableMongoDbOptions;
|
||||||
|
|
||||||
services
|
services
|
||||||
.AddOptions<MongoDbOptions>()
|
.AddOptions<MongoDbOptions>()
|
||||||
|
@ -21,10 +21,12 @@ public class MongoDbConnectionHandler
|
|||||||
|
|
||||||
private static IMongoDatabase CreateConnectionFromOptions(MongoDbOptions options)
|
private static IMongoDatabase CreateConnectionFromOptions(MongoDbOptions options)
|
||||||
{
|
{
|
||||||
var conn = new MongoClient(
|
var conn = new MongoClient(FormatConnectionString(options));
|
||||||
$"mongodb://{options.Username}:{options.Password}@{options.Host}:{options.Port}");
|
|
||||||
var database = conn.GetDatabase(options.Database);
|
var database = conn.GetDatabase(options.Database);
|
||||||
|
|
||||||
return database;
|
return database;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string FormatConnectionString(MongoDbOptions options) =>
|
||||||
|
$"mongodb://{options.Username}:{options.Password}@{options.Host}:{options.Port}";
|
||||||
}
|
}
|
@ -11,6 +11,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MongoDB.Driver" Version="2.13.2" />
|
<PackageReference Include="MongoDB.Driver" Version="2.13.2" />
|
||||||
<PackageReference Include="MongoDB.Driver.Core" Version="2.13.2" />
|
<PackageReference Include="MongoDB.Driver.Core" Version="2.13.2" />
|
||||||
|
<PackageReference Include="AspNetCore.HealthChecks.MongoDb" Version="5.0.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
Loading…
Reference in New Issue
Block a user