Health check

This commit is contained in:
Julian
2022-02-12 14:48:49 +01:00
parent 0e1b585db6
commit 9fa602664f
10 changed files with 94 additions and 6 deletions

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
@@ -34,11 +35,11 @@ namespace weddingapi
{
BsonSerializer.RegisterSerializer(new GuidSerializer(BsonType.String));
BsonSerializer.RegisterSerializer(new DateTimeOffsetSerializer(BsonType.String));
var mongoDbConfig = Configuration.GetSection(nameof(MongoDbConfig)).Get<MongoDbConfig>();
services.AddSingleton<IMongoClient>(serviceProvider =>
{
var config = Configuration.GetSection(nameof(MongoDbConfig)).Get<MongoDbConfig>();
return new MongoClient(config.ConnectionString);
return new MongoClient(mongoDbConfig.ConnectionString);
});
services.AddSingleton<IFamilyRepository, MongoDBFamilyRepository>();
@@ -49,6 +50,13 @@ namespace weddingapi
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "weddingapi", Version = "v1" });
});
services.AddHealthChecks()
.AddMongoDb(mongoDbConfig.ConnectionString,
name: "mongodb",
timeout: TimeSpan.FromSeconds(3),
tags: new[] { "ready" });
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -73,6 +81,14 @@ namespace weddingapi
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHealthChecks("/health/ready", new HealthCheckOptions {
Predicate = (check) => check.Tags.Contains("ready")
});
endpoints.MapHealthChecks("/health/live", new HealthCheckOptions {
Predicate = (_) => false
});
});
}
}