From 17677d4b01b24d59f9390e42fd9706fd3ebc3df9 Mon Sep 17 00:00:00 2001 From: Dominic NEED Date: Fri, 6 Oct 2023 10:10:23 +0100 Subject: [PATCH 1/3] Add OpenAPI documentation to health check endpoint --- .../Controllers/HealthCheckController.cs | 33 +++++++++++++++---- TramsDataApi/Startup.cs | 22 +++++++++++-- TramsDataApi/TramsDataApi.csproj | 1 + 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/TramsDataApi/Controllers/HealthCheckController.cs b/TramsDataApi/Controllers/HealthCheckController.cs index 1aea76981..b6ce89862 100644 --- a/TramsDataApi/Controllers/HealthCheckController.cs +++ b/TramsDataApi/Controllers/HealthCheckController.cs @@ -3,37 +3,58 @@ using Microsoft.Data.SqlClient; using Microsoft.Extensions.Logging; using TramsDataApi.DatabaseModels; +using Swashbuckle.AspNetCore.Annotations; namespace TramsDataApi.Controllers { + /// + /// Provides endpoints for checking the health of the API and database. + /// [ApiController] [Route("[controller]")] [ApiVersion("1.0")] + [SwaggerTag("Health Check Endpoints")] public class HealthCheckController : ControllerBase { private readonly LegacyTramsDbContext _dbContext; private readonly ILogger _logger; + /// + /// Constructor that initializes database and logger. + /// + /// Database context. + /// Logging service. public HealthCheckController(LegacyTramsDbContext context, ILogger logger) { _dbContext = context; _logger = logger; } - - + + /// + /// Provides a basic health check for the API. + /// + /// String message indicating health status. [HttpGet] + [SwaggerOperation(Summary = "Performs a basic health check", Description = "Checks if the API is operational.")] + [SwaggerResponse(200, "API is healthy.")] public string Get() { - _logger.LogInformation($"Returning OK Health Check"); + _logger.LogInformation("Returning OK Health Check"); return "Health check ok"; } - + + /// + /// Checks the database connectivity. + /// + /// Boolean indicating whether the database can be connected to. [HttpGet] [Route("/check_db")] + [SwaggerOperation(Summary = "Performs a database health check", Description = "Checks if the database is reachable.")] + [SwaggerResponse(200, "Database is healthy.")] public bool CheckDbConnection() { - _logger.LogInformation($"Returning Database Health Check"); + _logger.LogInformation("Returning Database Health Check"); return _dbContext.Database.CanConnect(); } } -} \ No newline at end of file +} diff --git a/TramsDataApi/Startup.cs b/TramsDataApi/Startup.cs index 0000e94ba..ef3b1b186 100644 --- a/TramsDataApi/Startup.cs +++ b/TramsDataApi/Startup.cs @@ -17,6 +17,9 @@ namespace TramsDataApi using UseCases; using TramsDataApi.SerilogCustomEnrichers; using TramsDataApi.ResponseModels; + using System.IO; + using System.Reflection; + using System; public class Startup { @@ -49,7 +52,14 @@ public void ConfigureServices(IServiceCollection services) services.AddScoped(); services.AddScoped(); services.AddScoped(); - services.AddScoped(); + services.AddScoped(); + + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); services.AddScoped(); services.AddScoped(); @@ -58,7 +68,7 @@ public void ConfigureServices(IServiceCollection services) services.AddScoped(); services.AddScoped(); - + services.AddScoped(); services.AddScoped(); @@ -73,7 +83,13 @@ public void ConfigureServices(IServiceCollection services) setup.GroupNameFormat = "'v'VVV"; setup.SubstituteApiVersionInUrl = true; }); - services.AddSwaggerGen(); + services.AddSwaggerGen(c => + { + var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; + var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); + c.IncludeXmlComments(xmlPath); + c.EnableAnnotations(); + }); services.ConfigureOptions(); services.AddUseCases(); diff --git a/TramsDataApi/TramsDataApi.csproj b/TramsDataApi/TramsDataApi.csproj index 5eac53670..8e123764f 100644 --- a/TramsDataApi/TramsDataApi.csproj +++ b/TramsDataApi/TramsDataApi.csproj @@ -22,6 +22,7 @@ + From 35808e9cadc49ec6c2a496b23f2a5dada125dd8d Mon Sep 17 00:00:00 2001 From: Dominic NEED Date: Fri, 6 Oct 2023 10:11:02 +0100 Subject: [PATCH 2/3] Added generate docs from summary comments in csproj --- TramsDataApi/TramsDataApi.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/TramsDataApi/TramsDataApi.csproj b/TramsDataApi/TramsDataApi.csproj index 8e123764f..991fcfe80 100644 --- a/TramsDataApi/TramsDataApi.csproj +++ b/TramsDataApi/TramsDataApi.csproj @@ -2,6 +2,7 @@ net6.0 1ce62ee2-ff0b-4f40-9066-cfbdae2e889f + true From 749457fcecb495763593e19e18e30cd795cc3171 Mon Sep 17 00:00:00 2001 From: Dominic NEED Date: Fri, 6 Oct 2023 10:17:57 +0100 Subject: [PATCH 3/3] Removed transfers DI --- TramsDataApi/Startup.cs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/TramsDataApi/Startup.cs b/TramsDataApi/Startup.cs index ef3b1b186..c5b11d28a 100644 --- a/TramsDataApi/Startup.cs +++ b/TramsDataApi/Startup.cs @@ -54,13 +54,6 @@ public void ConfigureServices(IServiceCollection services) services.AddScoped(); services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); services.AddScoped(); services.AddScoped();