Implement US-12 consolidated health overview service
This commit is contained in:
1 parent
cc76c9bde4
commit
076e675c81
26 files changed
+804
-16
No files matched your search
@@ -0,0 +1,78 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using HealthOverviewService.Application;
|
||||
using HealthOverviewService.Clients;
|
||||
|
||||
namespace HealthOverviewService;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddProblemDetails();
|
||||
builder.Services.AddScoped<HealthOverviewApplicationService>();
|
||||
builder.Services.AddHttpClient<PatientProfileServiceClient, HttpPatientProfileServiceClient>((services, client) =>
|
||||
{
|
||||
var configuration = services.GetRequiredService<IConfiguration>();
|
||||
client.BaseAddress = GetGatewayUri(configuration);
|
||||
var serviceKey = configuration["Services:ApiGateway:Key"];
|
||||
if (string.IsNullOrWhiteSpace(serviceKey) || serviceKey.Length < 32)
|
||||
throw new InvalidOperationException("Services:ApiGateway:Key must contain at least 32 characters.");
|
||||
client.DefaultRequestHeaders.Add("X-Service-Key", serviceKey);
|
||||
})
|
||||
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AllowAutoRedirect = false });
|
||||
builder.Services.AddHttpClient<ObservationServiceClient, HttpObservationServiceClient>((services, client) =>
|
||||
{
|
||||
var configuration = services.GetRequiredService<IConfiguration>();
|
||||
client.BaseAddress = GetGatewayUri(configuration);
|
||||
var serviceKey = configuration["Services:ApiGateway:Key"];
|
||||
if (string.IsNullOrWhiteSpace(serviceKey) || serviceKey.Length < 32)
|
||||
throw new InvalidOperationException("Services:ApiGateway:Key must contain at least 32 characters.");
|
||||
client.DefaultRequestHeaders.Add("X-Service-Key", serviceKey);
|
||||
})
|
||||
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AllowAutoRedirect = false });
|
||||
builder.Services.AddHttpClient<InsightsServiceClient, HttpInsightsServiceClient>((services, client) =>
|
||||
{
|
||||
var configuration = services.GetRequiredService<IConfiguration>();
|
||||
client.BaseAddress = GetGatewayUri(configuration);
|
||||
var serviceKey = configuration["Services:ApiGateway:Key"];
|
||||
if (string.IsNullOrWhiteSpace(serviceKey) || serviceKey.Length < 32)
|
||||
throw new InvalidOperationException("Services:ApiGateway:Key must contain at least 32 characters.");
|
||||
client.DefaultRequestHeaders.Add("X-Service-Key", serviceKey);
|
||||
})
|
||||
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AllowAutoRedirect = false });
|
||||
var app = builder.Build();
|
||||
if (!app.Environment.IsDevelopment() && GetGatewayUri(app.Configuration).Scheme != "https")
|
||||
throw new InvalidOperationException("Production gateway endpoints must use HTTPS.");
|
||||
_ = GetGatewayUri(app.Configuration);
|
||||
var outgoingKey = app.Configuration["Services:ApiGateway:Key"];
|
||||
if (string.IsNullOrWhiteSpace(outgoingKey) || outgoingKey.Length < 32)
|
||||
throw new InvalidOperationException("Services:ApiGateway:Key must contain at least 32 characters.");
|
||||
var gatewayKey = app.Configuration["InternalAccess:GatewayKey"];
|
||||
if (string.IsNullOrWhiteSpace(gatewayKey) || gatewayKey.Length < 32)
|
||||
throw new InvalidOperationException("InternalAccess:GatewayKey must contain at least 32 characters.");
|
||||
app.UseExceptionHandler();
|
||||
app.Use(async (context, next) =>
|
||||
{
|
||||
var supplied = context.Request.Headers["X-Gateway-Key"].ToString();
|
||||
if ((!app.Environment.IsDevelopment() && !context.Request.IsHttps) ||
|
||||
!CryptographicOperations.FixedTimeEquals(Encoding.UTF8.GetBytes(supplied), Encoding.UTF8.GetBytes(gatewayKey)))
|
||||
{ context.Response.StatusCode = 401; return; }
|
||||
await next(context);
|
||||
});
|
||||
app.MapControllers();
|
||||
app.Run();
|
||||
}
|
||||
|
||||
private static Uri GetGatewayUri(IConfiguration configuration)
|
||||
{
|
||||
var endpoint = configuration["Services:ApiGateway:BaseUrl"];
|
||||
if (!Uri.TryCreate(endpoint, UriKind.Absolute, out var uri) ||
|
||||
(uri.Scheme != "http" && uri.Scheme != "https") || uri.AbsolutePath != "/" ||
|
||||
!string.IsNullOrEmpty(uri.Query) || !string.IsNullOrEmpty(uri.Fragment) || !string.IsNullOrEmpty(uri.UserInfo))
|
||||
throw new InvalidOperationException("Services:ApiGateway:BaseUrl must be an HTTP(S) origin URL.");
|
||||
return uri;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user