Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Commit

Permalink
Added HealthCheck (#188)
Browse files Browse the repository at this point in the history
Checks home page.
  • Loading branch information
ardalis committed Jan 11, 2019
1 parent c64c70b commit 84f1eee
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
34 changes: 34 additions & 0 deletions src/Web/HealthChecks/HomePageHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -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.");
}
}
}
11 changes: 8 additions & 3 deletions src/Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.eShopWeb.Infrastructure.Identity;
using Microsoft.eShopWeb.Infrastructure.Logging;
using Microsoft.eShopWeb.Infrastructure.Services;
using Microsoft.eShopWeb.Web.HealthChecks;
using Microsoft.eShopWeb.Web.Interfaces;
using Microsoft.eShopWeb.Web.Services;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -113,13 +114,15 @@ public void ConfigureServices(IServiceCollection services)
new SlugifyParameterTransformer()));
}
).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);



services.AddHttpContextAccessor();
services.AddSwaggerGen(c =>
{
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
}

Expand Down Expand Up @@ -147,6 +150,8 @@ private static void ConfigureCookieSettings(IServiceCollection services)
// 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)
{
//app.UseDeveloperExceptionPage();
app.UseHealthChecks("/health");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
Expand Down

0 comments on commit 84f1eee

Please sign in to comment.