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
38 changes: 26 additions & 12 deletions EstateReportingAPI/Bootstrapper/MiddlewareRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,19 @@ namespace EstateReportingAPI.Bootstrapper{
public class MiddlewareRegistry : ServiceRegistry{
public MiddlewareRegistry()
{
this.ConfigureHealthChecks();
this.ConfigureSwagger();
this.ConfigureAuthentication();
this.ConfigureControllers();
this.ConfigureMiddlewareLogging();
this.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.PropertyNamingPolicy = new SnakeCaseNamingPolicy();
options.SerializerOptions.PropertyNameCaseInsensitive = true; // optional, but safer
});
}

private void ConfigureHealthChecks(){
var connectionStringSection = Startup.Configuration.GetSection("ConnectionStrings");
if (connectionStringSection.Exists() == false)
{
Expand All @@ -34,7 +46,9 @@ public MiddlewareRegistry()
failureStatus: HealthStatus.Degraded,
tags: new[] { "db", "sql", "sqlserver" });
}
}

private void ConfigureSwagger(){
this.AddSwaggerGen(c => {
c.SwaggerDoc("v1",
new OpenApiInfo{
Expand All @@ -61,7 +75,9 @@ public MiddlewareRegistry()
});

this.AddSwaggerExamplesFromAssemblyOf<SwaggerJsonConverter>();
}

private void ConfigureAuthentication(){
String? inTestMode = Environment.GetEnvironmentVariable("InTestMode");
if (String.Compare(inTestMode, Boolean.TrueString, StringComparison.InvariantCultureIgnoreCase) != 0){
this.AddAuthentication(options => {
Expand All @@ -79,16 +95,18 @@ public MiddlewareRegistry()
options.Audience = ConfigurationReader.GetValue("SecurityConfiguration", "ApiName");

options.TokenValidationParameters = new TokenValidationParameters{
ValidateAudience = false,
ValidAudience =
ConfigurationReader.GetValue("SecurityConfiguration", "ApiName"),
ValidIssuer =
ConfigurationReader.GetValue("SecurityConfiguration", "Authority"),
};
ValidateAudience = false,
ValidAudience =
ConfigurationReader.GetValue("SecurityConfiguration", "ApiName"),
ValidIssuer =
ConfigurationReader.GetValue("SecurityConfiguration", "Authority"),
};
options.IncludeErrorDetails = true;
});
}
}

private void ConfigureControllers(){
this.AddControllers().AddNewtonsoftJson(options => {
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
options.SerializerSettings.TypeNameHandling = TypeNameHandling.None;
Expand All @@ -99,7 +117,9 @@ public MiddlewareRegistry()

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

private void ConfigureMiddlewareLogging(){
bool logRequests = ConfigurationReader.GetValueOrDefault<Boolean>("MiddlewareLogging", "LogRequests", true);
bool logResponses = ConfigurationReader.GetValueOrDefault<Boolean>("MiddlewareLogging", "LogResponses", true);
LogLevel middlewareLogLevel = ConfigurationReader.GetValueOrDefault<LogLevel>("MiddlewareLogging", "MiddlewareLogLevel", LogLevel.Warning);
Expand All @@ -108,12 +128,6 @@ public MiddlewareRegistry()
new RequestResponseMiddlewareLoggingConfig(middlewareLogLevel, logRequests, logResponses);

this.AddSingleton(config);

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

private HttpClientHandler ApiEndpointHttpHandler(IServiceProvider serviceProvider){
Expand Down
Loading