Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Re-organise the app init code #440

Merged
merged 1 commit into from
Feb 23, 2024
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
45 changes: 34 additions & 11 deletions Childrens-Social-Care-CPD/WebApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public static void AddDependencies(this WebApplicationBuilder builder)

builder.Services.AddScoped<IContentLinkContext, ContentLinkContext>();

builder.Services.AddContentRenderers();
builder.Services.AddSearch();
AddContentRenderers(builder.Services);
AddSearch(builder.Services);
}

private static void AddContentRenderers(this IServiceCollection services)
private static void AddContentRenderers(IServiceCollection services)
{
// Register all the IRenderer<T> & IRendererWithOptions<T> implementations in the assembly
var assemblyTypes = System.Reflection.Assembly.GetExecutingAssembly().GetTypes();
Expand All @@ -78,7 +78,7 @@ private static void AddContentRenderers(this IServiceCollection services)
});
}

private static void AddSearch(this IServiceCollection services)
private static void AddSearch(IServiceCollection services)
{
services.AddScoped<ISearchResultsVMFactory, SearchResultsVMFactory>();

Expand All @@ -98,9 +98,20 @@ public static async Task AddFeatures(this WebApplicationBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
builder.Configuration.AddUserSecrets<Program>();
builder.Services.AddResponseCompression();
builder.Services.AddControllersWithViews();

var applicationConfiguration = new ApplicationConfiguration(builder.Configuration);

AddLogging(builder, applicationConfiguration);
AddContentful(builder, applicationConfiguration);
AddHostedServices(builder.Services);
AddHealthChecks(builder.Services);
await AddDataProtection(builder.Services, applicationConfiguration);
}

private static void AddLogging(WebApplicationBuilder builder, ApplicationConfiguration applicationConfiguration)
{
builder.Logging.AddAzureWebAppDiagnostics();
builder.Services.Configure<AzureFileLoggerOptions>(options =>
{
Expand All @@ -112,11 +123,6 @@ public static async Task AddFeatures(this WebApplicationBuilder builder)
options.BlobName = "log.txt";
});

builder.Services.AddResponseCompression();
builder.Services.AddControllersWithViews();
builder.Services.AddContentful(ContentfulConfiguration.GetContentfulConfiguration(builder.Configuration, applicationConfiguration));
builder.Services.AddHostedService<FeaturesConfigBackgroundService>();

var options = new ApplicationInsightsServiceOptions
{
ApplicationVersion = applicationConfiguration.AppVersion,
Expand All @@ -133,9 +139,26 @@ public static async Task AddFeatures(this WebApplicationBuilder builder)
options.Rules.Insert(0, new LoggerFilterRule(typeof(ApplicationInsightsLoggerProvider).FullName, null, LogLevel.Information, null));
}
});
}

builder.Services.AddHealthChecks().AddCheck<ConfigurationHealthCheck>("Configuration Health Check", tags: new[] {"configuration"});
private static void AddContentful(WebApplicationBuilder builder, ApplicationConfiguration applicationConfiguration)
{
var contentfulConfiguration = ContentfulConfiguration.GetContentfulConfiguration(builder.Configuration, applicationConfiguration);
builder.Services.AddContentful(contentfulConfiguration);
}

private static void AddHostedServices(IServiceCollection services)
{
services.AddHostedService<FeaturesConfigBackgroundService>();
}

private static void AddHealthChecks(IServiceCollection services)
{
services.AddHealthChecks().AddCheck<ConfigurationHealthCheck>("Configuration Health Check", tags: new[] { "configuration" });
}

private static async Task AddDataProtection(IServiceCollection services, ApplicationConfiguration applicationConfiguration)
{
if (!string.IsNullOrEmpty(applicationConfiguration.AzureDataProtectionContainerName))
{
var url = string.Format(applicationConfiguration.AzureStorageAccountUriFormatString,
Expand All @@ -149,7 +172,7 @@ public static async Task AddFeatures(this WebApplicationBuilder builder)
await blobContainerClient.CreateIfNotExistsAsync();

var blobUri = new Uri($"{url}/data-protection");
builder.Services
services
.AddDataProtection()
.SetApplicationName($"Childrens-Social-Care-CPD-{applicationConfiguration.AzureEnvironment}")
.PersistKeysToAzureBlobStorage(blobUri, managedIdentityCredential);
Expand Down
Loading