Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Healthcheck controller swagger #390

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading