-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fumika Koyama
committed
Oct 20, 2023
1 parent
ec6b223
commit 8566924
Showing
4 changed files
with
154 additions
and
3 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
samples/Dressca/dressca-backend/src/Dressca.Web/Controllers/HealthController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System.Net; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace Dressca.Web.Controllers; | ||
|
||
/// <summary> | ||
/// ヘルスチェック用のコントローラー。 | ||
/// </summary> | ||
[Route("api/health")] | ||
[ApiController] | ||
public class HealthController : ControllerBase | ||
{ | ||
private readonly HealthCheckService healthCheckService; | ||
|
||
/// <summary> | ||
/// コンストラクター。 | ||
/// </summary> | ||
/// <param name="healthCheckService">ヘルスチェックサービス。</param> | ||
public HealthController(HealthCheckService healthCheckService) | ||
{ | ||
this.healthCheckService = healthCheckService; | ||
} | ||
|
||
/// <summary> | ||
/// ヘルスチェックの結果を返します。 | ||
/// </summary> | ||
/// <returns>ヘルスチェックの結果。</returns> | ||
[HttpGet] | ||
public async Task<ActionResult> Get() | ||
{ | ||
HealthReport report = await this.healthCheckService.CheckHealthAsync(); | ||
|
||
var result = new | ||
{ | ||
status = report.Status.ToString(), | ||
result = report.Entries | ||
.Select(entry => new | ||
{ | ||
name = entry.Key, | ||
status = entry.Value.Status.ToString(), | ||
description = entry.Value.Description?.ToString(), | ||
data = entry.Value.Data, | ||
exception = new | ||
{ | ||
source = entry.Value.Exception?.Source, | ||
message = entry.Value.Exception?.Message, | ||
stackTrace = entry.Value.Exception?.StackTrace, | ||
}, | ||
}), | ||
}; | ||
return report.Status == HealthStatus.Healthy ? this.Ok(result) : this.StatusCode((int)HttpStatusCode.ServiceUnavailable, result); | ||
} | ||
|
||
/// <summary> | ||
/// ヘルスチェックの結果を返します。 | ||
/// </summary> | ||
/// <returns>ヘルスチェックの結果。</returns> | ||
[HttpHead] | ||
public async Task<ActionResult> Head() | ||
{ | ||
HealthReport report = await this.healthCheckService.CheckHealthAsync(); | ||
return report.Status == HealthStatus.Healthy ? this.Ok() : this.StatusCode((int)HttpStatusCode.ServiceUnavailable); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
samples/Dressca/dressca-backend/src/Dressca.Web/DbHealthCheck.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Data.Common; | ||
using Microsoft.Data.SqlClient; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace Dressca.Web; | ||
|
||
/// <summary> | ||
/// データベースのヘルスチェックを行うクラスです。 | ||
/// </summary> | ||
public class DbHealthCheck : IHealthCheck | ||
{ | ||
private string connectionString = @"Server=(localdb)\mssqllocaldb;Database=Dressca.Eshop;Integrated Security=True"; | ||
|
||
/// <summary> | ||
/// データベースへの接続を確認します。 | ||
/// </summary> | ||
/// <param name="context">ヘルスチェックコンテキスト。</param> | ||
/// <param name="cancellationToken">キャンセルトークン。</param> | ||
/// <returns>ヘルスチェックの結果。</returns> | ||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
using (var connection = new SqlConnection(this.connectionString)) | ||
{ | ||
try | ||
{ | ||
await connection.OpenAsync(cancellationToken); | ||
|
||
var command = connection.CreateCommand(); | ||
command.CommandText = "SELECT 1"; | ||
|
||
await command.ExecuteNonQueryAsync(cancellationToken); | ||
} | ||
catch (DbException ex) | ||
{ | ||
return new HealthCheckResult(status: context.Registration.FailureStatus, exception: ex); | ||
} | ||
} | ||
|
||
return HealthCheckResult.Healthy(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters