-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: SOFTURE COMMON HEALTHCHECK - RELEASE NEW VERSION TO NUGET.ORG | ||
|
||
on: | ||
release: | ||
types: [released] | ||
|
||
jobs: | ||
publishing: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: ⚙️ Install dotnet | ||
uses: actions/setup-dotnet@v1 | ||
with: | ||
dotnet-version: 8.0.100 | ||
|
||
- name: 🔗 Restore dependencies | ||
run: dotnet restore ./API | ||
|
||
- name: 📂 Create new nuget package | ||
run: dotnet pack --no-restore -c Release -o ./artifacts /p:PackageVersion=${{ github.ref_name }} /p:Version=${{ github.ref_name }} ./COMMON/SOFTURE.Common.HealthCheck/SOFTURE.Common.HealthCheck.csproj | ||
continue-on-error: false | ||
|
||
- name: 🚀 Push new nuget package | ||
run: dotnet nuget push ./artifacts/SOFTURE.Common.HealthCheck.${{ github.ref_name }}.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SOFTURE.Common.HealthCheck", "SOFTURE.Common.HealthCheck\SOFTURE.Common.HealthCheck.csproj", "{5D6F7130-2C65-4BB3-A81C-5975E9CEEE9A}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{5D6F7130-2C65-4BB3-A81C-5975E9CEEE9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{5D6F7130-2C65-4BB3-A81C-5975E9CEEE9A}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{5D6F7130-2C65-4BB3-A81C-5975E9CEEE9A}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{5D6F7130-2C65-4BB3-A81C-5975E9CEEE9A}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using CSharpFunctionalExtensions; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace SOFTURE.Common.HealthCheck.Core; | ||
|
||
public abstract class CheckBase() : ICommonHealthCheck | ||
{ | ||
private string Name => GetType().Name.Replace("HealthCheck", string.Empty); | ||
|
||
protected readonly CancellationTokenSource Cts = new(TimeSpan.FromSeconds(2)); | ||
|
||
protected abstract Task<Result> Check(); | ||
|
||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken ct = new()) | ||
{ | ||
var data = new Dictionary<string, object>(); | ||
|
||
try | ||
{ | ||
var result = await Check(); | ||
|
||
if (result.IsFailure) | ||
data.Add("Error", result.Error); | ||
|
||
return result.IsSuccess | ||
? HealthCheckResult.Healthy($"{Name} is available") | ||
: HealthCheckResult.Unhealthy($"{Name} is not available", data: data); | ||
} | ||
catch (Exception ex) | ||
{ | ||
data.Add("Exception", ex.InnerException?.Message ?? ex.Message); | ||
|
||
return HealthCheckResult.Unhealthy($"{Name} is not available", data: data); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace SOFTURE.Common.HealthCheck.Core; | ||
|
||
internal static class Consts | ||
{ | ||
public const string HealthCheckTag = "softure"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace SOFTURE.Common.HealthCheck.Core; | ||
|
||
public interface ICommonHealthCheck : IHealthCheck | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SOFTURE.Common.HealthCheck.Core; | ||
using SOFTURE.Common.HealthCheck.Presentation; | ||
|
||
namespace SOFTURE.Common.HealthCheck | ||
{ | ||
public static class DependencyInjection | ||
{ | ||
public static IServiceCollection AddCommonHealthCheck<THealthCheck>(this IServiceCollection services) | ||
where THealthCheck : CheckBase, ICommonHealthCheck | ||
{ | ||
var healthCheckName = typeof(THealthCheck).Name.Replace("HealthCheck", string.Empty); | ||
|
||
services.AddHealthChecks() | ||
.AddCheck<THealthCheck>(healthCheckName, tags: new[] { Consts.HealthCheckTag }); | ||
|
||
return services; | ||
} | ||
|
||
public static WebApplication MapCommonHealthChecks(this WebApplication app) | ||
{ | ||
app.MapHealthChecks("/hc", new HealthCheckOptions | ||
{ | ||
ResponseWriter = Writer.WriteResponse, | ||
Predicate = healthCheck => healthCheck.Tags.Contains(Consts.HealthCheckTag) | ||
}); | ||
|
||
return app; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace SOFTURE.Common.HealthCheck.Presentation; | ||
|
||
internal static class Writer | ||
{ | ||
internal static Task WriteResponse(HttpContext context, HealthReport healthReport) | ||
{ | ||
context.Response.ContentType = "application/json; charset=utf-8"; | ||
|
||
var options = new JsonWriterOptions { Indented = true }; | ||
|
||
using var memoryStream = new MemoryStream(); | ||
using (var jsonWriter = new Utf8JsonWriter(memoryStream, options)) | ||
{ | ||
jsonWriter.WriteStartObject(); | ||
jsonWriter.WriteString("Status", healthReport.Status.ToString()); | ||
jsonWriter.WriteStartObject("Results"); | ||
|
||
foreach (var healthReportEntry in healthReport.Entries) | ||
{ | ||
jsonWriter.WriteStartObject(healthReportEntry.Key); | ||
jsonWriter.WriteString("Status", healthReportEntry.Value.Status.ToString()); | ||
jsonWriter.WriteString("Description", healthReportEntry.Value.Description); | ||
|
||
if (healthReportEntry.Value.Data.Count > 0) | ||
{ | ||
jsonWriter.WriteStartObject("Information"); | ||
|
||
foreach(var data in healthReportEntry.Value.Data) | ||
jsonWriter.WriteString(data.Key, data.Value.ToString()); | ||
|
||
jsonWriter.WriteEndObject(); | ||
} | ||
|
||
jsonWriter.WriteEndObject(); | ||
} | ||
|
||
jsonWriter.WriteEndObject(); | ||
jsonWriter.WriteEndObject(); | ||
} | ||
|
||
return context.Response.WriteAsync(Encoding.UTF8.GetString(memoryStream.ToArray())); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
API/SOFTURE.Common.HealthCheck/SOFTURE.Common.HealthCheck.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1" /> | ||
<PackageReference Include="SOFTURE.Results" Version="0.0.7" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<AssemblyName>SOFTURE.Common.HealthCheck</AssemblyName> | ||
<AssemblyTitle>$(AssemblyName)</AssemblyTitle> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<LangVersion>latest</LangVersion> | ||
<Title>$(AssemblyName)</Title> | ||
<Authors>SOFTURE</Authors> | ||
<Copyright>Copyright (c) 2024 $(Authors)</Copyright> | ||
<Description>SOFTURE - HealtCheck</Description> | ||
<EmbedUntrackedSources>true</EmbedUntrackedSources> | ||
<IncludeSymbols>true</IncludeSymbols> | ||
<PackageId>$(AssemblyName)</PackageId> | ||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | ||
<PackageProjectUrl>https://github.com/SOFTURE/API</PackageProjectUrl> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
<PackageReleaseNotes>See $(PackageProjectUrl)/blob/master/CHANGELOG.md for release notes.</PackageReleaseNotes> | ||
<PackageTags>SOFTURE</PackageTags> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<RepositoryType>Git</RepositoryType> | ||
<RepositoryUrl>$(PackageProjectUrl)</RepositoryUrl> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\README.md" Pack="true" PackagePath="\"/> | ||
<None Include="..\..\LICENSE" Pack="true" PackagePath="\"/> | ||
</ItemGroup> | ||
|
||
</Project> |