forked from CarmenAPuccio/GeoLocationAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
78 lines (62 loc) · 2.58 KB
/
Program.cs
File metadata and controls
78 lines (62 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using GeoLocationAPI.V1.HealthChecks;
using GeoLocationAPI.V1.Services;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddSingleton<IGeoLocationService, GeoLocationService>();
builder.Services.AddHealthChecks()
.AddTypeActivatedCheck<GeoLocationHealthCheck>(
"GeoLocationHealthCheck",
args: [
builder.Configuration.GetValue<string>("HealtcheckBaseURL"),
builder.Configuration.GetValue<string>("HealtcheckIPToTest")
]
);
var app = builder.Build();
app.MapControllers();
app.MapHealthChecks("/hc", new HealthCheckOptions
{
ResponseWriter = WriteResponse
});
app.Run();
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.WriteString("Duration", healthReport.TotalDuration.ToString());
jsonWriter.WriteString("FrameworkDescription", RuntimeInformation.FrameworkDescription);
jsonWriter.WriteString("ProcessArchitecture", RuntimeInformation.ProcessArchitecture.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()));
}