From e3f4e8c9450b541bd270a6663e76a07581f4b1d3 Mon Sep 17 00:00:00 2001 From: Vanderlan Gomes Date: Tue, 10 Oct 2023 18:55:15 -0300 Subject: [PATCH] Add Elastic Search and SQL Server Health checks (#23) --- Orion.Api/Bootstrapper.cs | 5 ++- .../Configuration/HealthCheckConfiguration.cs | 32 +++++++++++++++++++ Orion.Api/Orion.Api.csproj | 3 ++ Orion.Test/API/HealthCheckApiTest.cs | 26 --------------- .../Configuration/ApiTestInitializer.cs | 7 ++-- README.md | 2 +- 6 files changed, 40 insertions(+), 35 deletions(-) create mode 100644 Orion.Api/Configuration/HealthCheckConfiguration.cs delete mode 100644 Orion.Test/API/HealthCheckApiTest.cs diff --git a/Orion.Api/Bootstrapper.cs b/Orion.Api/Bootstrapper.cs index 63d8674..ab226a2 100644 --- a/Orion.Api/Bootstrapper.cs +++ b/Orion.Api/Bootstrapper.cs @@ -27,7 +27,7 @@ public static void ConfigureApp(this IApplicationBuilder app) app.ConfigureGlobalization(); - app.UseHealthChecks("/health-check"); + app.ConfigureHealthCheck(); app.UseRouting(); @@ -59,14 +59,13 @@ public static void ConfigureServices(this IServiceCollection services, IConfigur .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix) .AddDataAnnotationsLocalization(); - services.Configure(options => { options.SuppressModelStateInvalidFilter = true; }); services.AddLocalization(options => options.ResourcesPath = @"Resources"); - services.AddHealthChecks(); + services.AddApplicationHealthChecks(configuration); services.ConfigureSwagger(); services.ConfigureApiVersioning(); diff --git a/Orion.Api/Configuration/HealthCheckConfiguration.cs b/Orion.Api/Configuration/HealthCheckConfiguration.cs new file mode 100644 index 0000000..d511c2d --- /dev/null +++ b/Orion.Api/Configuration/HealthCheckConfiguration.cs @@ -0,0 +1,32 @@ +using HealthChecks.UI.Client; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; + +namespace Orion.Api.Configuration +{ + public static class HealthCheckConfiguration + { + public static void ConfigureHealthCheck(this IApplicationBuilder app) + { + app.UseHealthChecks("/health", new HealthCheckOptions + { + ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse + }); + } + + public static void AddApplicationHealthChecks(this IServiceCollection services, IConfiguration configuration) + { + services.AddHealthChecks().AddSqlServer(configuration["DatabaseOptions:ConnectionString"], + tags: new[] + { + "database" + }); + + services.AddHealthChecks().AddElasticsearch(configuration["ElasticConfiguration:Uri"], + tags: new[] + { + "elasticsearch", + "kibana" + }); + } + } +} diff --git a/Orion.Api/Orion.Api.csproj b/Orion.Api/Orion.Api.csproj index 326fe20..411a86d 100644 --- a/Orion.Api/Orion.Api.csproj +++ b/Orion.Api/Orion.Api.csproj @@ -9,6 +9,9 @@ + + + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Orion.Test/API/HealthCheckApiTest.cs b/Orion.Test/API/HealthCheckApiTest.cs deleted file mode 100644 index 66e3ef4..0000000 --- a/Orion.Test/API/HealthCheckApiTest.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System.Net; -using System.Threading.Tasks; -using Orion.Test.Configuration; -using Xunit; - -namespace Orion.Test.API -{ - public class HealthCheckApiTest: ApiTestInitializer - { - [Fact] - public async Task GetAsync_HealthCheck_ReturnsHealthy() - { - //arrange - var successMessageService = "Healthy"; - - //act - var result = await Client.GetAsync("/health-check"); - - //assert - var content = await result.Content.ReadAsStringAsync(); - - Assert.Equal(HttpStatusCode.OK, result.StatusCode); - Assert.Equal(successMessageService, content); - } - } -} diff --git a/Orion.Test/Configuration/ApiTestInitializer.cs b/Orion.Test/Configuration/ApiTestInitializer.cs index c21d332..7844094 100644 --- a/Orion.Test/Configuration/ApiTestInitializer.cs +++ b/Orion.Test/Configuration/ApiTestInitializer.cs @@ -12,7 +12,7 @@ namespace Orion.Test.Configuration public abstract class ApiTestInitializer : IDisposable { protected string AuthToken; - protected readonly HttpClient Client; + protected readonly HttpClient Client; protected readonly HttpClient AuthenticatedClient; protected IServiceProvider ServiceProvider { get; private set; } @@ -26,10 +26,7 @@ public ApiTestInitializer() .Build(); builder - .UseConfiguration(config) - .ConfigureServices(services => - { - }); + .UseConfiguration(config); }); ServiceProvider = appFactory.Services; Client = appFactory.CreateClient(); diff --git a/README.md b/README.md index 9b6c162..3df03e8 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ **About this Project** -*A project reference for creating REST APIs with C# and .NET (v7.0).* +*A simple project template for creating a .NET API (v7.0)* The main objective is to start projects with a clean and simple architecture, without having to redo the entire configuration whenever starting a new project with similar characteristics.