Skip to content

Commit

Permalink
Merge pull request #390 from DFE-Digital/healthcheck-controller-swagger
Browse files Browse the repository at this point in the history
Healthcheck controller swagger
  • Loading branch information
dneed-nimble authored Oct 6, 2023
2 parents 2f9d10f + 749457f commit 1f2135d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
33 changes: 27 additions & 6 deletions TramsDataApi/Controllers/HealthCheckController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,58 @@
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using TramsDataApi.DatabaseModels;
using Swashbuckle.AspNetCore.Annotations;

namespace TramsDataApi.Controllers
{
/// <summary>
/// Provides endpoints for checking the health of the API and database.
/// </summary>
[ApiController]
[Route("[controller]")]
[ApiVersion("1.0")]
[SwaggerTag("Health Check Endpoints")]
public class HealthCheckController : ControllerBase
{
private readonly LegacyTramsDbContext _dbContext;
private readonly ILogger<HealthCheckController> _logger;

/// <summary>
/// Constructor that initializes database and logger.
/// </summary>
/// <param name="context">Database context.</param>
/// <param name="logger">Logging service.</param>
public HealthCheckController(LegacyTramsDbContext context, ILogger<HealthCheckController> logger)
{
_dbContext = context;
_logger = logger;
}



/// <summary>
/// Provides a basic health check for the API.
/// </summary>
/// <returns>String message indicating health status.</returns>
[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";
}


/// <summary>
/// Checks the database connectivity.
/// </summary>
/// <returns>Boolean indicating whether the database can be connected to.</returns>
[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();
}
}
}
}
15 changes: 12 additions & 3 deletions TramsDataApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -49,7 +52,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddScoped<IGetEstablishmentsByTrustUid, GetEstablishmentsByTrustUid>();
services.AddScoped<IGetEstablishments, GetEstablishments>();
services.AddScoped<ISearchTrusts, SearchTrusts>();
services.AddScoped<IMstrSearchTrusts, MasterSearchTrusts>();
services.AddScoped<IMstrSearchTrusts, MasterSearchTrusts>();

services.AddScoped<IGetKeyStagePerformanceByUrn, GetKeyStagePerformanceByUrn>();
services.AddScoped<IEducationPerformanceGateway, EducationPerformanceGateway>();
Expand All @@ -58,7 +61,7 @@ public void ConfigureServices(IServiceCollection services)

services.AddScoped<IFssProjectGateway, FssProjectGateway>();
services.AddScoped<IGetAllFssProjects, GetAllFssProjects>();

services.AddScoped<IGetAllFssProjects, GetAllFssProjects>();
services.AddScoped<ICorrelationContext, CorrelationContext>();

Expand All @@ -73,7 +76,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<SwaggerOptions>();
services.AddUseCases();

Expand Down
2 changes: 2 additions & 0 deletions TramsDataApi/TramsDataApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<UserSecretsId>1ce62ee2-ff0b-4f40-9066-cfbdae2e889f</UserSecretsId>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CsvHelper" Version="30.0.1" />
Expand All @@ -22,6 +23,7 @@
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageReference Include="TimeZoneConverter" Version="6.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
Expand Down

0 comments on commit 1f2135d

Please sign in to comment.