From 9f3073e9acdfb664ab2393b9bab6fa1208f4b5fa Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 13 Jan 2022 16:51:19 -0600 Subject: [PATCH] add more docs, update readme --- README.md | 9 +++++++++ .../HealthChecks/BasicHealthCheck.cs | 2 +- .../BasicHealthCheckWithUptime.cs | 5 ++--- TinyHealthCheck/Models/HealthCheckResult.cs | 19 ++++--------------- TinyHealthCheck/Models/IHealthCheckResult.cs | 11 +++++++++++ .../Models/JsonHealthCheckResult.cs | 12 +++--------- 6 files changed, 30 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 4779762..2b30c89 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,15 @@ Once 10 iterations have been exceeded, the response will change: A complete example can be found in the `DummyServiceWorker` directory. +## Response Interface + +The `IHealthCheckResult` interface is used for returning the response data to the client. Two concrete result types are included: + +- An open-ended `HealthCheckResult` that requires you to serialize the payload however you require +- The `JsonHealthCheckResult` accepts an object and automatically serializes it into JSON + +Inheiriting from the `IHealthCheckResult` makes it easy to create a custom implementation to return a response body of any serialization scheme. + ## Hostname consideration By default, the `hostname` parameter is set to `localhost`. This will work fine for local development, but will not work across the network. diff --git a/TinyHealthCheck/HealthChecks/BasicHealthCheck.cs b/TinyHealthCheck/HealthChecks/BasicHealthCheck.cs index 3348ce5..c4477d2 100644 --- a/TinyHealthCheck/HealthChecks/BasicHealthCheck.cs +++ b/TinyHealthCheck/HealthChecks/BasicHealthCheck.cs @@ -1,5 +1,4 @@ using System.Net; -using System.Text.Json; using System.Threading; using System.Threading.Tasks; using TinyHealthCheck.Models; @@ -12,6 +11,7 @@ namespace TinyHealthCheck.HealthChecks public class BasicHealthCheck : IHealthCheck { #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously + /// public async Task ExecuteAsync(CancellationToken cancellationToken) => new JsonHealthCheckResult(new { Status = "Healthy!" }, HttpStatusCode.OK); #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously diff --git a/TinyHealthCheck/HealthChecks/BasicHealthCheckWithUptime.cs b/TinyHealthCheck/HealthChecks/BasicHealthCheckWithUptime.cs index cda527a..c8c0196 100644 --- a/TinyHealthCheck/HealthChecks/BasicHealthCheckWithUptime.cs +++ b/TinyHealthCheck/HealthChecks/BasicHealthCheckWithUptime.cs @@ -1,7 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; -using System; +using System; using System.Net; -using System.Text.Json; using System.Threading; using System.Threading.Tasks; using TinyHealthCheck.Models; @@ -16,6 +14,7 @@ public class BasicHealthCheckWithUptime : IHealthCheck private DateTimeOffset processStartTime = DateTimeOffset.Now; #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously + /// public async Task ExecuteAsync(CancellationToken cancellationToken) #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously { diff --git a/TinyHealthCheck/Models/HealthCheckResult.cs b/TinyHealthCheck/Models/HealthCheckResult.cs index 053039b..9331fce 100644 --- a/TinyHealthCheck/Models/HealthCheckResult.cs +++ b/TinyHealthCheck/Models/HealthCheckResult.cs @@ -2,18 +2,11 @@ namespace TinyHealthCheck.Models { - /// - /// Container to hold the response data for an IHealthCheck. Make sure to serialze the Body object in to the specified ContentType. - /// + /// public class HealthCheckResult : IHealthCheckResult { /// - /// Container to hold the response data for an IHealthCheck. Make sure to serialze the Body object in to the specified ContentType. - /// - public HealthCheckResult() { } - - /// - /// Container to hold the response data for an IHealthCheck. Make sure to serialze the Body object in to the specified ContentType. + /// Container to hold the response data for an IHealthCheck. Make sure to serialize the Body object in to the specified ContentType. /// public HealthCheckResult(string body, HttpStatusCode statusCode) { @@ -21,14 +14,10 @@ public HealthCheckResult(string body, HttpStatusCode statusCode) StatusCode = statusCode; } - /// - /// Serialized response body. Make sure the serialization scheme matches the ContentType defined for the health-check. - /// + /// public string Body { get; set; } - /// - /// HTTP response code that will be returned to the client - /// + /// public HttpStatusCode StatusCode { get; set; } = HttpStatusCode.OK; } } diff --git a/TinyHealthCheck/Models/IHealthCheckResult.cs b/TinyHealthCheck/Models/IHealthCheckResult.cs index cb05958..bd53ac7 100644 --- a/TinyHealthCheck/Models/IHealthCheckResult.cs +++ b/TinyHealthCheck/Models/IHealthCheckResult.cs @@ -2,9 +2,20 @@ namespace TinyHealthCheck.Models { + /// + /// Container to hold the response data for an IHealthCheck. + /// Make sure to serialze the Body object in to the ContentType that the health check is configured for. + /// public interface IHealthCheckResult { + /// + /// Serialized response body. Make sure the serialization scheme matches the ContentType defined for the health-check. + /// string Body { get; set; } + + /// + /// HTTP response code that will be returned to the client + /// HttpStatusCode StatusCode { get; set; } } } \ No newline at end of file diff --git a/TinyHealthCheck/Models/JsonHealthCheckResult.cs b/TinyHealthCheck/Models/JsonHealthCheckResult.cs index c429286..3832a5b 100644 --- a/TinyHealthCheck/Models/JsonHealthCheckResult.cs +++ b/TinyHealthCheck/Models/JsonHealthCheckResult.cs @@ -3,9 +3,7 @@ namespace TinyHealthCheck.Models { - /// - /// Container to hold the response data for an IHealthCheck. Automatically serializes the response object into JSON. - /// + /// public class JsonHealthCheckResult : IHealthCheckResult { /// @@ -17,14 +15,10 @@ public JsonHealthCheckResult(object responseObject, HttpStatusCode statusCode) StatusCode = statusCode; } - /// - /// JSON response body - /// + /// public string Body { get; set; } - /// - /// HTTP response code that will be returned to the client - /// + /// public HttpStatusCode StatusCode { get; set; } = HttpStatusCode.OK; } }