-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
76 lines (61 loc) · 2.38 KB
/
Program.cs
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
using System.Text.Json;
using UME.Fhir.Converter;
var appName = "UME.Fhir.Converter";
// nice rececipes https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-7.0
var builder = WebApplication.CreateBuilder(args);
// Configure logging
builder.Logging.AddConsole();
builder.Logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Information);
var app = builder.Build();
// Get a logger
var logger = app.Services.GetRequiredService<ILogger<Program>>();
app.Use(async (context, next) =>
{
context.Response.OnStarting(() =>
{
context.Response.Headers["Server"] = appName;
return Task.CompletedTask;
});
await next.Invoke();
});
app.MapGet("/", () => {
return $"{appName}";
});
app.MapGet("/probe", (HttpContext context) => {
logger.LogInformation($"Liveness probe was called from {context.Connection.RemoteIpAddress}");
return $"alive at {DateTimeOffset.Now.ToString()}";
});
/**
* Make conversion available as a custom FHIR operation.
* Try to be compliant with https://github.com/microsoft/fhir-server/blob/main/docs/ConvertDataOperation.md
* see also https://build.fhir.org/operations.html
*/
app.MapPost("/$convert-data", async (HttpContext context) =>
{
try {
var parameters = await context.Request.ReadFromJsonAsync<ConvertParameters>();
var parsedParameters = new ParsedConvertParameters(
inputData: parameters.GetParameter("inputData"),
inputDataType: parameters.GetParameter("inputDataType"),
rootTemplate: parameters.GetParameter("rootTemplate")
);
// convert to JSON FHIR
var converterResult = ConverterLogicHandler.Convert(
inputContent: parsedParameters.inputData,
inputDataType: parsedParameters.inputDataType,
rootTemplate: parsedParameters.rootTemplate,
isTraceInfo: false);
await context.WriteSuccess(converterResult.FhirResource.ToString());
}
catch (ParameterNotFoundException ex) {
await context.WriteError(400, ex);
}
catch (JsonException ex) {
await context.WriteError(400, ex);
}
catch (Exception ex) {
Console.WriteLine(ex.ToString());
await context.WriteError(500, ex);
}
});
app.Run();