Adding API Health Check (#191)
This commit is contained in:
40
src/Web/HealthChecks/ApiHealthCheck.cs
Normal file
40
src/Web/HealthChecks/ApiHealthCheck.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.eShopWeb.Web.HealthChecks
|
||||
{
|
||||
public class ApiHealthCheck : IHealthCheck
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly LinkGenerator _linkGenerator;
|
||||
|
||||
public ApiHealthCheck(IHttpContextAccessor httpContextAccessor, LinkGenerator linkGenerator)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_linkGenerator = linkGenerator;
|
||||
}
|
||||
|
||||
public async Task<HealthCheckResult> CheckHealthAsync(
|
||||
HealthCheckContext context,
|
||||
CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
var request = _httpContextAccessor.HttpContext.Request;
|
||||
|
||||
string apiLink = _linkGenerator.GetPathByAction("List", "Catalog");
|
||||
string myUrl = request.Scheme + "://" + request.Host.ToString() + apiLink;
|
||||
var client = new HttpClient();
|
||||
var response = await client.GetAsync(myUrl);
|
||||
var pageContents = await response.Content.ReadAsStringAsync();
|
||||
if (pageContents.Contains(".NET Bot Black Sweatshirt"))
|
||||
{
|
||||
return HealthCheckResult.Healthy("The check indicates a healthy result.");
|
||||
}
|
||||
|
||||
return HealthCheckResult.Unhealthy("The check indicates an unhealthy result.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,9 @@ namespace Microsoft.eShopWeb.Web.HealthChecks
|
||||
HealthCheckContext context,
|
||||
CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
string myUrl = _httpContextAccessor.HttpContext.Request.Host.ToString();
|
||||
var request = _httpContextAccessor.HttpContext.Request;
|
||||
string myUrl = request.Scheme + "://" + request.Host.ToString();
|
||||
|
||||
var client = new HttpClient();
|
||||
var response = await client.GetAsync(myUrl);
|
||||
var pageContents = await response.Content.ReadAsStringAsync();
|
||||
|
||||
Reference in New Issue
Block a user