Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 77 additions & 50 deletions TransactionProcessor/Bootstrapper/MiddlewareRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,45 +42,25 @@ public MiddlewareRegistry()
String connectionString = Startup.Configuration.GetValue<String>("EventStoreSettings:ConnectionString");
KurrentDBClientSettings eventStoreClientSettings = KurrentDBClientSettings.Create(connectionString);

this.AddHealthChecks()
.AddEventStore(eventStoreClientSettings,
userCredentials: eventStoreClientSettings.DefaultCredentials,
name:"Eventstore",
failureStatus:HealthStatus.Unhealthy,
tags:new[] {"db", "eventstore"}).AddSecurityService(this.ApiEndpointHttpHandler);

this.AddSwaggerGen(c =>
{
c.CustomSchemaIds(type => type.FullName); // Uses the full namespace as schema ID
c.SwaggerDoc("v1",
new OpenApiInfo
{
Title = "Transaction Processor API",
Version = "1.0",
Description = "A REST Api to manage the processing and routing of transactions to local and remote operators.",
Contact = new OpenApiContact
{
Name = "Stuart Ferguson",
Email = "golfhandicapping@btinternet.com"
}
});
// add a custom operation filter which sets default values
c.OperationFilter<SwaggerDefaultValues>();
c.ExampleFilters();
this.ConfigureHealthChecks(eventStoreClientSettings);
this.ConfigureSwagger();
this.ConfigureAuthentication();
this.ConfigureControllers();
this.ConfigureMvc();
this.ConfigureAuthorization();
this.ConfigureJsonOptions();
}

//Locate the XML files being generated by ASP.NET...
var directory = new DirectoryInfo(AppContext.BaseDirectory);
var xmlFiles = directory.GetFiles("*.xml");
#endregion

//... and tell Swagger to use those XML comments.
foreach (FileInfo fileInfo in xmlFiles)
{
c.IncludeXmlComments(fileInfo.FullName);
}
});
private String GetSecurityServiceConfigValue(String keyName) {
return ConfigurationReader.GetValue("SecurityConfiguration", keyName);
}

this.AddSwaggerExamplesFromAssemblyOf<SwaggerJsonConverter>();
#region Methods

private void ConfigureAuthentication()
{
IdentityModelEventSource.ShowPII = true;

this.AddAuthentication(options =>
Expand All @@ -103,14 +83,21 @@ public MiddlewareRegistry()
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = false,
ValidAudience =
GetSecurityServiceConfigValue("ApiName"),
ValidIssuer =
GetSecurityServiceConfigValue("Authority"),
ValidAudience = GetSecurityServiceConfigValue("ApiName"),
ValidIssuer = GetSecurityServiceConfigValue("Authority"),
};
options.IncludeErrorDetails = true;
});
}

private void ConfigureAuthorization()
{
this.AddClientCredentialsOnlyPolicy();
this.AddClientCredentialsHandler();
}

private void ConfigureControllers()
{
this.AddControllers().AddNewtonsoftJson(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
Expand All @@ -119,27 +106,67 @@ public MiddlewareRegistry()
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
}

Assembly assembly = this.GetType().GetTypeInfo().Assembly;
this.AddMvcCore().AddApplicationPart(assembly).AddControllersAsServices();

this.AddClientCredentialsOnlyPolicy();
this.AddClientCredentialsHandler();
private void ConfigureHealthChecks(KurrentDBClientSettings eventStoreClientSettings)
{
this.AddHealthChecks()
.AddEventStore(eventStoreClientSettings,
userCredentials: eventStoreClientSettings.DefaultCredentials,
name: "Eventstore",
failureStatus: HealthStatus.Unhealthy,
tags: new[] { "db", "eventstore" }).AddSecurityService(this.ApiEndpointHttpHandler);
}

private void ConfigureJsonOptions()
{
this.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.PropertyNamingPolicy = new SnakeCaseNamingPolicy();
options.SerializerOptions.PropertyNameCaseInsensitive = true; // optional, but safer
});
}

#endregion

private String GetSecurityServiceConfigValue(String keyName) {
return ConfigurationReader.GetValue("SecurityConfiguration", keyName);
private void ConfigureMvc()
{
Assembly assembly = this.GetType().GetTypeInfo().Assembly;
this.AddMvcCore().AddApplicationPart(assembly).AddControllersAsServices();
}

#region Methods
private void ConfigureSwagger()
{
this.AddSwaggerGen(c =>
{
c.CustomSchemaIds(type => type.FullName); // Uses the full namespace as schema ID
c.SwaggerDoc("v1",
new OpenApiInfo
{
Title = "Transaction Processor API",
Version = "1.0",
Description = "A REST Api to manage the processing and routing of transactions to local and remote operators.",
Contact = new OpenApiContact
{
Name = "Stuart Ferguson",
Email = "golfhandicapping@btinternet.com"
}
});
// add a custom operation filter which sets default values
c.OperationFilter<SwaggerDefaultValues>();
c.ExampleFilters();

//Locate the XML files being generated by ASP.NET...
var directory = new DirectoryInfo(AppContext.BaseDirectory);
var xmlFiles = directory.GetFiles("*.xml");

//... and tell Swagger to use those XML comments.
foreach (FileInfo fileInfo in xmlFiles)
{
c.IncludeXmlComments(fileInfo.FullName);
}
});

this.AddSwaggerExamplesFromAssemblyOf<SwaggerJsonConverter>();
}

/// <summary>
/// APIs the endpoint HTTP handler.
Expand All @@ -162,4 +189,4 @@ private HttpClientHandler ApiEndpointHttpHandler(IServiceProvider serviceProvide

#endregion
}
}
}
Loading