mirror of
https://github.com/Jetsparrow/antiantiswearingbot.git
synced 2026-01-20 23:16:08 +03:00
53 lines
1.8 KiB
C#
53 lines
1.8 KiB
C#
using System.IO;
|
|
using System.Text.Json;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
|
|
namespace Jetsparrow.Aasb.Health;
|
|
|
|
public static class HealthUtils
|
|
{
|
|
|
|
public static Task WriteHealthCheckResponse(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);
|
|
jsonWriter.WriteStartObject("data");
|
|
|
|
foreach (var item in healthReportEntry.Value.Data)
|
|
{
|
|
jsonWriter.WritePropertyName(item.Key);
|
|
|
|
JsonSerializer.Serialize(jsonWriter, item.Value,
|
|
item.Value?.GetType() ?? typeof(object));
|
|
}
|
|
|
|
jsonWriter.WriteEndObject();
|
|
jsonWriter.WriteEndObject();
|
|
}
|
|
|
|
jsonWriter.WriteEndObject();
|
|
jsonWriter.WriteEndObject();
|
|
}
|
|
|
|
return context.Response.WriteAsync(
|
|
Encoding.UTF8.GetString(memoryStream.ToArray()));
|
|
}
|
|
}
|