From 5894226e93253ff9d8b9cede4a6a72872925e39f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Domeradzki?= Date: Tue, 16 Apr 2024 10:37:32 +0200 Subject: [PATCH] Add optional health checks to ASF API --- ArchiSteamFarm/IPC/ArchiKestrel.cs | 3 + .../Controllers/Api/HealthCheckController.cs | 58 +++++++++++++++++++ .../IPC/Responses/HealthCheckResponse.cs | 46 +++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 ArchiSteamFarm/IPC/Controllers/Api/HealthCheckController.cs create mode 100644 ArchiSteamFarm/IPC/Responses/HealthCheckResponse.cs diff --git a/ArchiSteamFarm/IPC/ArchiKestrel.cs b/ArchiSteamFarm/IPC/ArchiKestrel.cs index 198492543..310c1bfc1 100644 --- a/ArchiSteamFarm/IPC/ArchiKestrel.cs +++ b/ArchiSteamFarm/IPC/ArchiKestrel.cs @@ -377,6 +377,9 @@ internal static class ArchiKestrel { } ); + // Add support for optional healtchecks + services.AddHealthChecks(); + // We need MVC for /Api, but we're going to use only a small subset of all available features IMvcBuilder mvc = services.AddControllers(); diff --git a/ArchiSteamFarm/IPC/Controllers/Api/HealthCheckController.cs b/ArchiSteamFarm/IPC/Controllers/Api/HealthCheckController.cs new file mode 100644 index 000000000..c8ee2ea8d --- /dev/null +++ b/ArchiSteamFarm/IPC/Controllers/Api/HealthCheckController.cs @@ -0,0 +1,58 @@ +// ---------------------------------------------------------------------------------------------- +// _ _ _ ____ _ _____ +// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ +// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ +// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| +// ---------------------------------------------------------------------------------------------- +// | +// Copyright 2015-2024 Łukasz "JustArchi" Domeradzki +// Contact: JustArchi@JustArchi.net +// | +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// | +// http://www.apache.org/licenses/LICENSE-2.0 +// | +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Net; +using System.Threading; +using System.Threading.Tasks; +using ArchiSteamFarm.IPC.Responses; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Diagnostics.HealthChecks; + +namespace ArchiSteamFarm.IPC.Controllers.Api; + +[ApiController] +[Produces("application/json")] +[Route("/HealthCheck")] +public sealed class HealthCheckController : ControllerBase { + private readonly HealthCheckService HealthCheckService; + + public HealthCheckController(HealthCheckService healthCheckService) { + ArgumentNullException.ThrowIfNull(healthCheckService); + + HealthCheckService = healthCheckService; + } + + [HttpGet] + [ProducesResponseType(typeof(HealthCheckResponse), (int) HttpStatusCode.OK)] + [ProducesResponseType(typeof(HealthCheckResponse), (int) HttpStatusCode.ServiceUnavailable)] + public async Task Get() { + CancellationToken cancellationToken = HttpContext.RequestAborted; + + HealthReport report = await HealthCheckService.CheckHealthAsync(cancellationToken).ConfigureAwait(false); + + HealthCheckResponse response = new(report); + + return report.Status == HealthStatus.Healthy ? Ok(response) : StatusCode((int) HttpStatusCode.ServiceUnavailable, response); + } +} diff --git a/ArchiSteamFarm/IPC/Responses/HealthCheckResponse.cs b/ArchiSteamFarm/IPC/Responses/HealthCheckResponse.cs new file mode 100644 index 000000000..97c16d09f --- /dev/null +++ b/ArchiSteamFarm/IPC/Responses/HealthCheckResponse.cs @@ -0,0 +1,46 @@ +// ---------------------------------------------------------------------------------------------- +// _ _ _ ____ _ _____ +// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ +// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ +// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| +// ---------------------------------------------------------------------------------------------- +// | +// Copyright 2015-2024 Łukasz "JustArchi" Domeradzki +// Contact: JustArchi@JustArchi.net +// | +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// | +// http://www.apache.org/licenses/LICENSE-2.0 +// | +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.ComponentModel.DataAnnotations; +using System.Text.Json.Serialization; +using Microsoft.Extensions.Diagnostics.HealthChecks; + +namespace ArchiSteamFarm.IPC.Responses; + +public sealed class HealthCheckResponse { + [JsonInclude] + [Required] + public string StatusText => Status.ToString(); + + [JsonInclude] + [JsonRequired] + [Required] + public HealthStatus Status { get; private init; } + + internal HealthCheckResponse(HealthReport report) { + ArgumentNullException.ThrowIfNull(report); + + Status = report.Status; + } +}