11using Api . Settings ;
2- using Infrastructure . Constants ;
32using Infrastructure . Data ;
43using Microsoft . AspNetCore . Diagnostics . HealthChecks ;
54using Microsoft . Extensions . Diagnostics . HealthChecks ;
@@ -21,56 +20,43 @@ IConfiguration configuration
2120 HealthCheckSettings healthCheckSettings =
2221 configuration . GetSection ( nameof ( HealthCheckSettings ) ) . Get < HealthCheckSettings > ( )
2322 ?? new ( ) ;
24- services
25- . AddHealthChecks ( )
26- . AddNpgSql (
27- provider =>
28- {
29- var databaseSetting = provider
30- . GetRequiredService < IOptions < DatabaseSettings > > ( )
31- . Value ;
32- CurrentProvider currentProvider = databaseSetting . Provider ;
23+ IHealthChecksBuilder builder = services . AddHealthChecks ( ) ;
3324
34- if (
35- DatabaseConfiguration . relationalProviders . Contains (
36- currentProvider . ToString ( )
37- )
38- )
39- {
40- return currentProvider switch
41- {
42- CurrentProvider . PostgreSQL => databaseSetting
43- . Relational !
44- . PostgreSQL !
45- . ConnectionString ,
46- _ => throw new NotSupportedException (
47- $ "Database provider { currentProvider } is not supported."
48- ) ,
49- } ;
50- }
51- return currentProvider switch
25+ CurrentProvider ? provider = configuration
26+ . GetSection ( $ "{ nameof ( DatabaseSettings ) } :{ nameof ( DatabaseSettings . Provider ) } ")
27+ . Get < CurrentProvider > ( ) ;
28+
29+ switch ( provider )
30+ {
31+ case CurrentProvider . PostgreSQL :
32+ builder . AddNpgSql (
33+ ( sp ) =>
5234 {
53- CurrentProvider . PostgreSQL => databaseSetting
54- . NonRelational !
55- . MongoDB !
56- . ConnectionString ,
57- _ => throw new NotSupportedException (
58- $ "Database provider { currentProvider } is not supported."
59- ) ,
60- } ;
61- } ,
62- name : "postgres" ,
63- failureStatus : HealthStatus . Unhealthy ,
64- tags : [ "db" , "postgres" ] ,
65- timeout : TimeSpan . FromSeconds ( healthCheckSettings . TimeoutSeconds )
66- ) ;
35+ var databaseSetting = sp . GetRequiredService <
36+ IOptions < DatabaseSettings >
37+ > ( ) . Value ;
38+ return databaseSetting . Relational ! . PostgreSQL ! . ConnectionString ;
39+ } ,
40+ name : "postgres" ,
41+ failureStatus : HealthStatus . Unhealthy ,
42+ tags : [ "db" , "postgres" ] ,
43+ timeout : TimeSpan . FromSeconds ( healthCheckSettings . TimeoutSeconds )
44+ ) ;
45+ break ;
46+
47+ case CurrentProvider . MongoDB :
48+ //
49+ break ;
50+ default :
51+ throw new NotSupportedException ( $ "Database provider { provider } is not supported.") ;
52+ }
6753 }
6854
69- public static void UseHealthCheck ( this WebApplication app , IConfiguration configuration )
55+ public static void UseHealthCheck ( this WebApplication app )
7056 {
71- HealthCheckSettings healthCheckSettings =
72- configuration . GetSection ( nameof ( HealthCheckSettings ) ) . Get < HealthCheckSettings > ( )
73- ?? new ( ) ;
57+ HealthCheckSettings healthCheckSettings = app
58+ . Services . GetRequiredService < IOptions < HealthCheckSettings > > ( )
59+ . Value ;
7460 app . MapHealthChecks (
7561 healthCheckSettings . Path ,
7662 new HealthCheckOptions
@@ -84,13 +70,13 @@ public static void UseHealthCheck(this WebApplication app, IConfiguration config
8470 {
8571 status = report . Status . ToString ( ) ,
8672 totalDuration = report . TotalDuration . ToString ( ) ,
87- entries = report . Entries . Select ( x => new
73+ entries = report . Entries . Select ( entry => new
8874 {
89- key = x . Key ,
90- status = x . Value . Status . ToString ( ) ,
91- description = x . Value . Description ,
92- duration = x . Value . Duration . ToString ( ) ,
93- data = x . Value . Data ,
75+ name = entry . Key ,
76+ status = entry . Value . Status . ToString ( ) ,
77+ description = entry . Value . Description ,
78+ duration = entry . Value . Duration . ToString ( ) ,
79+ data = entry . Value . Data ,
9480 } ) ,
9581 } ;
9682
0 commit comments