Merge pull request #1471 from dennisreimann/api-health-endpoint

GreenField: Add health check endpoint
This commit is contained in:
Nicolas Dorier
2020-04-23 02:44:11 +09:00
committed by GitHub
5 changed files with 109 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Client.Models;
namespace BTCPayServer.Client
{
public partial class BTCPayServerClient
{
public virtual async Task<ApiHealthData> GetHealth(CancellationToken token = default)
{
var response = await _httpClient.SendAsync(CreateHttpRequest("api/v1/health"), token);
return await HandleResponse<ApiHealthData>(response);
}
}
}

View File

@@ -0,0 +1,10 @@
using BTCPayServer.Client.JsonConverters;
using Newtonsoft.Json;
namespace BTCPayServer.Client.Models
{
public class ApiHealthData
{
public bool Synchronized { get; set; }
}
}

View File

@@ -272,5 +272,20 @@ namespace BTCPayServer.Tests
}
}
[Fact(Timeout = TestTimeout)]
[Trait("Integration", "Integration")]
public async Task HealthControllerTests()
{
using (var tester = ServerTester.Create())
{
await tester.StartAsync();
var unauthClient = new BTCPayServerClient(tester.PayTester.ServerUri);
var apiHealthData = await unauthClient.GetHealth();
Assert.NotNull(apiHealthData);
Assert.True(apiHealthData.Synchronized);
}
}
}
}

View File

@@ -0,0 +1,22 @@
using BTCPayServer.HostedServices;
using BTCPayServer.Client.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace BTCPayServer.Controllers.GreenField
{
[Controller]
public class HealthController : ControllerBase
{
[AllowAnonymous]
[HttpGet("~/api/v1/health")]
public ActionResult GetHealth(NBXplorerDashboard dashBoard)
{
ApiHealthData model = new ApiHealthData()
{
Synchronized = dashBoard.IsFullySynched()
};
return Ok(model);
}
}
}

View File

@@ -0,0 +1,46 @@
{
"paths": {
"/api/v1/health": {
"get": {
"tags": [
"Health"
],
"summary": "Get health status",
"description": "Check the instance health status",
"operationId": "Health_GetHealth",
"responses": {
"200": {
"description": "Instance is up",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ApplicationHealthData"
}
}
}
}
},
"security": []
}
}
},
"components": {
"schemas": {
"ApplicationHealthData": {
"type": "object",
"additionalProperties": false,
"properties": {
"synchronized": {
"type": "boolean",
"description": "True if the instance is fully synchronized, according to NBXplorer"
}
}
}
}
},
"tags": [
{
"name": "Health"
}
]
}