diff --git a/TransactionProcessor/Bootstrapper/MiddlewareRegistry.cs b/TransactionProcessor/Bootstrapper/MiddlewareRegistry.cs index 575c5e36..5a1053dd 100644 --- a/TransactionProcessor/Bootstrapper/MiddlewareRegistry.cs +++ b/TransactionProcessor/Bootstrapper/MiddlewareRegistry.cs @@ -42,45 +42,25 @@ public MiddlewareRegistry() String connectionString = Startup.Configuration.GetValue("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(); - 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(); + #region Methods + private void ConfigureAuthentication() + { IdentityModelEventSource.ShowPII = true; this.AddAuthentication(options => @@ -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; @@ -119,13 +106,20 @@ 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(); @@ -133,13 +127,46 @@ public MiddlewareRegistry() }); } - #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(); + 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(); + } /// /// APIs the endpoint HTTP handler. @@ -162,4 +189,4 @@ private HttpClientHandler ApiEndpointHttpHandler(IServiceProvider serviceProvide #endregion } -} \ No newline at end of file +}