Added HealthCheck (#188)

Checks home page.
This commit is contained in:
Steve Smith
2019-01-11 13:27:15 -05:00
committed by GitHub
parent c64c70b183
commit 84f1eeee8c
2 changed files with 42 additions and 3 deletions

View File

@@ -0,0 +1,34 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.eShopWeb.Web.HealthChecks
{
public class HomePageHealthCheck : IHealthCheck
{
private readonly IHttpContextAccessor _httpContextAccessor;
public HomePageHealthCheck(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public async Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context,
CancellationToken cancellationToken = default(CancellationToken))
{
string myUrl = _httpContextAccessor.HttpContext.Request.Host.ToString();
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.");
}
}
}

View File

@@ -13,6 +13,7 @@ using Microsoft.eShopWeb.Infrastructure.Data;
using Microsoft.eShopWeb.Infrastructure.Identity; using Microsoft.eShopWeb.Infrastructure.Identity;
using Microsoft.eShopWeb.Infrastructure.Logging; using Microsoft.eShopWeb.Infrastructure.Logging;
using Microsoft.eShopWeb.Infrastructure.Services; using Microsoft.eShopWeb.Infrastructure.Services;
using Microsoft.eShopWeb.Web.HealthChecks;
using Microsoft.eShopWeb.Web.Interfaces; using Microsoft.eShopWeb.Web.Interfaces;
using Microsoft.eShopWeb.Web.Services; using Microsoft.eShopWeb.Web.Services;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@@ -113,13 +114,15 @@ namespace Microsoft.eShopWeb.Web
new SlugifyParameterTransformer())); new SlugifyParameterTransformer()));
} }
).SetCompatibilityVersion(CompatibilityVersion.Version_2_2); ).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddHttpContextAccessor();
services.AddSwaggerGen(c => services.AddSwaggerGen(c =>
{ {
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" }); c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
}); });
services.AddHealthChecks()
.AddCheck<HomePageHealthCheck>("home_page_health_check");
_services = services; // used to debug registered services _services = services; // used to debug registered services
} }
@@ -147,6 +150,8 @@ namespace Microsoft.eShopWeb.Web
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, LinkGenerator linkGenerator) public void Configure(IApplicationBuilder app, IHostingEnvironment env, LinkGenerator linkGenerator)
{ {
//app.UseDeveloperExceptionPage();
app.UseHealthChecks("/health");
if (env.IsDevelopment()) if (env.IsDevelopment())
{ {
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();