Skip to content

Commit

Permalink
add more docs, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
bruceharrison1984 committed Jan 13, 2022
1 parent 50f636d commit 9f3073e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 28 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion TinyHealthCheck/HealthChecks/BasicHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Net;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using TinyHealthCheck.Models;
Expand All @@ -12,6 +11,7 @@ namespace TinyHealthCheck.HealthChecks
public class BasicHealthCheck : IHealthCheck
{
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
/// <inheritdoc/>
public async Task<IHealthCheckResult> ExecuteAsync(CancellationToken cancellationToken) =>
new JsonHealthCheckResult(new { Status = "Healthy!" }, HttpStatusCode.OK);
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
Expand Down
5 changes: 2 additions & 3 deletions TinyHealthCheck/HealthChecks/BasicHealthCheckWithUptime.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
/// <inheritdoc/>
public async Task<IHealthCheckResult> ExecuteAsync(CancellationToken cancellationToken)
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
Expand Down
19 changes: 4 additions & 15 deletions TinyHealthCheck/Models/HealthCheckResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,22 @@

namespace TinyHealthCheck.Models
{
/// <summary>
/// Container to hold the response data for an IHealthCheck. Make sure to serialze the Body object in to the specified ContentType.
/// </summary>
/// <inheritdoc/>
public class HealthCheckResult : IHealthCheckResult
{
/// <summary>
/// Container to hold the response data for an IHealthCheck. Make sure to serialze the Body object in to the specified ContentType.
/// </summary>
public HealthCheckResult() { }

/// <summary>
/// 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.
/// </summary>
public HealthCheckResult(string body, HttpStatusCode statusCode)
{
Body = body;
StatusCode = statusCode;
}

/// <summary>
/// Serialized response body. Make sure the serialization scheme matches the ContentType defined for the health-check.
/// </summary>
/// <inheritdoc/>
public string Body { get; set; }

/// <summary>
/// HTTP response code that will be returned to the client
/// </summary>
/// <inheritdoc/>
public HttpStatusCode StatusCode { get; set; } = HttpStatusCode.OK;
}
}
11 changes: 11 additions & 0 deletions TinyHealthCheck/Models/IHealthCheckResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@

namespace TinyHealthCheck.Models
{
/// <summary>
/// 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.
/// </summary>
public interface IHealthCheckResult
{
/// <summary>
/// Serialized response body. Make sure the serialization scheme matches the ContentType defined for the health-check.
/// </summary>
string Body { get; set; }

/// <summary>
/// HTTP response code that will be returned to the client
/// </summary>
HttpStatusCode StatusCode { get; set; }
}
}
12 changes: 3 additions & 9 deletions TinyHealthCheck/Models/JsonHealthCheckResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

namespace TinyHealthCheck.Models
{
/// <summary>
/// Container to hold the response data for an IHealthCheck. Automatically serializes the response object into JSON.
/// </summary>
/// <inheritdoc/>
public class JsonHealthCheckResult : IHealthCheckResult
{
/// <summary>
Expand All @@ -17,14 +15,10 @@ public JsonHealthCheckResult(object responseObject, HttpStatusCode statusCode)
StatusCode = statusCode;
}

/// <summary>
/// JSON response body
/// </summary>
/// <inheritdoc/>
public string Body { get; set; }

/// <summary>
/// HTTP response code that will be returned to the client
/// </summary>
/// <inheritdoc/>
public HttpStatusCode StatusCode { get; set; } = HttpStatusCode.OK;
}
}

0 comments on commit 9f3073e

Please sign in to comment.