Skip to content

Commit

Permalink
fix: deleted startup, usage comes from extension file with various im…
Browse files Browse the repository at this point in the history
…plementations
  • Loading branch information
robinaasan committed Dec 16, 2024
1 parent 2b0efac commit 1a67dff
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 43 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using Asp.Versioning;
using Equinor.SubSurfAppManagementMonitoringNuGet.Config;
using Equinor.SubSurfAppManagementMonitoringNuGet.Models;
using Equinor.SubSurfAppManagementMonitoringNuGet.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace Equinor.SubSurfAppManagementMonitoringNuGet.Controllers;

Expand All @@ -20,12 +18,13 @@ public class HealthController : ControllerBase
{
private readonly IHealthCheckerService _healthCheckerService;

private readonly ApplicationConfig _applicationConfig;
private readonly HealthControllerOptions _options;

public HealthController(IHealthCheckerService healthCheckerService, IOptions<ApplicationConfig> applicationConfig)

public HealthController(IHealthCheckerService healthCheckerService, HealthControllerOptions options)
{
_healthCheckerService = healthCheckerService;
_applicationConfig = applicationConfig.Value;
_options = options;
}

/// <summary>
Expand All @@ -35,7 +34,7 @@ public HealthController(IHealthCheckerService healthCheckerService, IOptions<App
[HttpGet]
public async Task<ApplicationHealth> Get()
{
var applicationHealth = await _healthCheckerService.CheckHealthAsync(_applicationConfig.AppName);
var applicationHealth = await _healthCheckerService.CheckHealthAsync(_options.AppName);

return applicationHealth;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Equinor.SubSurfAppManagementMonitoringNuGet.Controllers;

/// <summary>
/// Represents options that can be passed to the health controller.
/// </summary>
public class HealthControllerOptions
{
/// <summary>
/// A custom parameter to configure the health controller.
/// </summary>
public string AppName { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,58 @@
using Equinor.SubSurfAppManagementMonitoringNuGet.Controllers;
using Equinor.SubSurfAppManagementMonitoringNuGet.Helpers;
using Equinor.SubSurfAppManagementMonitoringNuGet.Models;
using Equinor.SubSurfAppManagementMonitoringNuGet.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Equinor.SubSurfAppManagementMonitoringNuGet.Extensions;

public static class HealthCheckerServiceCollectionExtensions
{
/// <summary>
/// Adds health checks and a health controller to the application, while configuring a custom health checker service.
/// </summary>
/// <typeparam name="T">The type of the custom health checker service that implements <see cref="IHealthCheckerService"/>.</typeparam>
/// <param name="services">The service collection to add the health services to.</param>
/// <param name="configuration">The application configuration used to configure the health services.</param>
/// <returns>An <see cref="IHealthChecksBuilder"/> to further configure health checks.</returns>
/// <exception cref="ArgumentException">Thrown if <typeparamref name="T"/> does not implement <see cref="IHealthCheckerService"/>.</exception>
public static IHealthChecksBuilder AddCustomHealthController<T>(this IServiceCollection services, IConfiguration configuration)
where T : class, IHealthCheckerService
{
services.AddMvc().AddApplicationPart(typeof(HealthController).Assembly);
var healthChecksBuilder = services.ConfigureHealthServices<T>(configuration);
return healthChecksBuilder;
}

/// <summary>
/// Adds the default health checker service and integrates it with the health check builder.
/// </summary>
/// <param name="services">The service collection.</param>
public static IHealthChecksBuilder AddDefaultHealthChecker(this IServiceCollection services)
/// <param name="configuration"></param>
/// <param name="applicationName">The name of your application, will show up as the applicationName in the <see cref="ApplicationHealth"/> response</param>
public static IHealthChecksBuilder AddDefaultHealthController(this IServiceCollection services, IConfiguration configuration, string applicationName)
{
services.AddSingleton<IEnvironment, EnvironmentWrapper>();
services.AddSingleton(new HealthControllerOptions { AppName = applicationName });
services.AddMvc().AddApplicationPart(typeof(HealthController).Assembly);
var healthChecksBuilder = services.ConfigureHealthServices<DefaultHealthCheckerService>(configuration);
return healthChecksBuilder;
}

/// <summary>
/// Configures health-related services, including a custom health checker service and the health controller.
/// </summary>
/// <typeparam name="T">The type of the custom health checker service that implements <see cref="IHealthCheckerService"/>.</typeparam>
/// <param name="services">The service collection to configure.</param>
/// <param name="configuration">The application configuration used to configure the health services.</param>
public static IHealthChecksBuilder ConfigureHealthServices<T>(this IServiceCollection services, IConfiguration configuration)
where T : class, IHealthCheckerService
{
services.AddTransient<IHealthCheckerService, T>();
var healthChecksBuilder = services.AddHealthChecks();
return healthChecksBuilder;
}


}
27 changes: 0 additions & 27 deletions Equinor.SubSurfAppManagementMonitoringNuGet/Startup.cs

This file was deleted.

0 comments on commit 1a67dff

Please sign in to comment.