From 84f1eeee8c3980746bb7f18bcabfdc781443db19 Mon Sep 17 00:00:00 2001 From: Steve Smith Date: Fri, 11 Jan 2019 13:27:15 -0500 Subject: [PATCH] Added HealthCheck (#188) Checks home page. --- src/Web/HealthChecks/HomePageHealthCheck.cs | 34 +++++++++++++++++++++ src/Web/Startup.cs | 11 +++++-- 2 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 src/Web/HealthChecks/HomePageHealthCheck.cs diff --git a/src/Web/HealthChecks/HomePageHealthCheck.cs b/src/Web/HealthChecks/HomePageHealthCheck.cs new file mode 100644 index 0000000..a8f322a --- /dev/null +++ b/src/Web/HealthChecks/HomePageHealthCheck.cs @@ -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 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."); + } + } +} diff --git a/src/Web/Startup.cs b/src/Web/Startup.cs index 8fe097c..2e34f80 100644 --- a/src/Web/Startup.cs +++ b/src/Web/Startup.cs @@ -13,6 +13,7 @@ using Microsoft.eShopWeb.Infrastructure.Data; 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; @@ -113,13 +114,15 @@ namespace Microsoft.eShopWeb.Web 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("home_page_health_check"); + _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. public void Configure(IApplicationBuilder app, IHostingEnvironment env, LinkGenerator linkGenerator) { + //app.UseDeveloperExceptionPage(); + app.UseHealthChecks("/health"); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage();