11using System ;
22using System . Linq ;
3+ using System . Threading . Tasks ;
34using Micro . Starter . Api . Configs ;
5+ using Micro . Starter . Api . HealthCheck ;
46using Micro . Starter . Api . Models ;
57using Micro . Starter . Api . Repository ;
68using Micro . Starter . Api . Uuid ;
79using Micro . Starter . Api . Workers ;
810using Microsoft . AspNetCore . Builder ;
11+ using Microsoft . AspNetCore . Diagnostics . HealthChecks ;
912using Microsoft . AspNetCore . Hosting ;
13+ using Microsoft . AspNetCore . Http ;
1014using Microsoft . Extensions . Configuration ;
1115using Microsoft . Extensions . DependencyInjection ;
16+ using Microsoft . Extensions . Diagnostics . HealthChecks ;
1217using Microsoft . Extensions . Hosting ;
1318using Microsoft . Extensions . Logging ;
1419using Microsoft . Extensions . Logging . Slack ;
1520using Microsoft . Extensions . Options ;
1621using Microsoft . OpenApi . Models ;
22+ using Newtonsoft . Json ;
23+ using Newtonsoft . Json . Linq ;
1724
1825namespace Micro . Starter . Api
1926{
@@ -32,6 +39,7 @@ public void ConfigureServices(IServiceCollection services)
3239 AddConfiguration ( services , Configuration ) ;
3340 services . AddMetrics ( ) ;
3441 ConfigureDependencies ( services ) ;
42+ ConfigureHealthChecks ( services , Configuration ) ;
3543 services . AddControllers ( ) ;
3644 services . AddSwaggerGen ( c =>
3745 {
@@ -44,6 +52,13 @@ public void ConfigureServices(IServiceCollection services)
4452 } ) ;
4553 RegisterWorker ( services ) ;
4654 }
55+ private static void ConfigureHealthChecks ( IServiceCollection services , IConfiguration configuration )
56+ {
57+ services
58+ . AddHealthChecks ( )
59+ . AddCheck < ConnectionToDbCheck > ( nameof ( ConnectionToDbCheck ) )
60+ . AddCheck < MemoryCheck > ( nameof ( MemoryCheck ) ) ;
61+ }
4762
4863 private static void ConfigureDependencies ( IServiceCollection services )
4964 {
@@ -82,7 +97,30 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
8297 x . RoutePrefix = "swagger" ;
8398 x . SwaggerEndpoint ( "/swagger/v1/swagger.json" , "V1" ) ;
8499 } ) ;
85- app . UseEndpoints ( endpoints => { endpoints . MapControllers ( ) ; } ) ;
100+ app . UseEndpoints ( endpoints =>
101+ {
102+ endpoints . MapControllers ( ) ;
103+ endpoints . MapHealthChecks ( "/health" , new HealthCheckOptions
104+ {
105+ ResponseWriter = WriteResponse ,
106+ AllowCachingResponses = false ,
107+ } ) ;
108+ } ) ;
109+ }
110+ private static Task WriteResponse ( HttpContext httpContext , HealthReport result )
111+ {
112+ httpContext . Response . ContentType = "application/json" ;
113+
114+ var json = new JObject (
115+ new JProperty ( "status" , result . Status . ToString ( ) ) ,
116+ new JProperty ( "results" , new JObject ( result . Entries . Select ( pair =>
117+ new JProperty ( pair . Key , new JObject (
118+ new JProperty ( "status" , pair . Value . Status . ToString ( ) ) ,
119+ new JProperty ( "description" , pair . Value . Description ) ,
120+ new JProperty ( "data" , new JObject ( pair . Value . Data . Select (
121+ p => new JProperty ( p . Key , p . Value ) ) ) ) ) ) ) ) ) ) ;
122+ return httpContext . Response . WriteAsync (
123+ json . ToString ( Formatting . Indented ) ) ;
86124 }
87125
88126 private static void ConfigureSlack ( ILoggerFactory loggerFactory , SlackLoggingConfig slackConfig , IWebHostEnvironment env )
0 commit comments