mirror of
https://github.com/freedbygrace/ActiveDirectoryToRestApi.git
synced 2026-07-26 11:28:20 +00:00
Add Health controller
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
<UserSecretsId>d52504e9-ea6e-4152-a52e-467ca002e47c</UserSecretsId>
|
||||
<AssemblyVersion>$(PackageVersion)</AssemblyVersion>
|
||||
<FileVersion>$(PackageVersion)</FileVersion>
|
||||
<Version>1.0.0.1</Version>
|
||||
<Version>1.0.0.2</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
namespace ActiveDirectoryToRestApi.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("/")]
|
||||
public class HealthController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<HealthController> _logger;
|
||||
private readonly LdapService _ldapService;
|
||||
|
||||
public HealthController(ILogger<HealthController> logger, LdapService ldapService)
|
||||
{
|
||||
_logger = logger;
|
||||
_ldapService = ldapService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public ActionResult<Health> Get()
|
||||
{
|
||||
var connection = _ldapService.CheckLdapConnection();
|
||||
if (connection) return Ok(new Health());
|
||||
return StatusCode(500, new Health { Data = new HealthData { Health = false } });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace ActiveDirectoryToRestApi.Models;
|
||||
|
||||
public class Health
|
||||
{
|
||||
public HealthData Data { get; set; } = new();
|
||||
}
|
||||
|
||||
public class HealthData
|
||||
{
|
||||
public bool Health { get; set; } = true;
|
||||
}
|
||||
@@ -2,15 +2,18 @@
|
||||
|
||||
public class LdapService
|
||||
{
|
||||
private readonly ILogger<LdapService> _logger;
|
||||
private readonly LdapConfigs _ldapConfigs;
|
||||
private readonly LdapConnection _connection;
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
public LdapService(LdapConnection connection, IConfiguration configuration)
|
||||
public LdapService(ILogger<LdapService> logger, LdapConnection connection, IConfiguration configuration)
|
||||
{
|
||||
_logger = logger;
|
||||
_connection = connection;
|
||||
_configuration = configuration;
|
||||
_ldapConfigs = _configuration.GetSection(nameof(LdapConfigs)).Get<LdapConfigs>();
|
||||
_logger = logger;
|
||||
}
|
||||
public User GetUserByUserName(string username)
|
||||
{
|
||||
@@ -174,4 +177,18 @@ public class LdapService
|
||||
return 42;
|
||||
}
|
||||
}
|
||||
|
||||
public bool CheckLdapConnection()
|
||||
{
|
||||
try
|
||||
{
|
||||
_connection.Bind();
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("LDAP connection failed: {ex.StackTrace}", ex.StackTrace);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user