Skip to content

Commit

Permalink
Convert to file-scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r committed Jul 30, 2023
1 parent 4607fc5 commit dcfb0b7
Show file tree
Hide file tree
Showing 128 changed files with 5,390 additions and 5,517 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = false

# C# formatting settings - Namespace options
csharp_style_namespace_declarations = file_scoped:suggestion
csharp_style_namespace_declarations = file_scoped:warning

########## name all private fields using camelCase with underscore prefix ##########
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-naming-conventions?view=vs-2019
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
using HealthChecks.UI.Image.Configuration.Helpers;

namespace HealthChecks.UI.Image.Configuration
namespace HealthChecks.UI.Image.Configuration;

public class AzureAppConfiguration
{
public class AzureAppConfiguration
public static bool Enabled
{
public static bool Enabled
get
{
get
{
return EnvironmentVariable.HasValue(AzureAppConfigurationKeys.Enabled) &&
bool.TryParse(EnvironmentVariable.GetValue(AzureAppConfigurationKeys.Enabled), out bool enabled) &&
enabled;
}
return EnvironmentVariable.HasValue(AzureAppConfigurationKeys.Enabled) &&
bool.TryParse(EnvironmentVariable.GetValue(AzureAppConfigurationKeys.Enabled), out bool enabled) &&
enabled;
}
}

public static bool UseConnectionString =>
EnvironmentVariable.HasValue(AzureAppConfigurationKeys.ConnectionString);
public static bool UseConnectionString =>
EnvironmentVariable.HasValue(AzureAppConfigurationKeys.ConnectionString);

public static bool UseLabel =>
EnvironmentVariable.HasValue(AzureAppConfigurationKeys.Label);
public static bool UseLabel =>
EnvironmentVariable.HasValue(AzureAppConfigurationKeys.Label);

public static bool UseCacheExpiration =>
EnvironmentVariable.HasValue(AzureAppConfigurationKeys.CacheExpiration)
&& double.TryParse(EnvironmentVariable.GetValue(AzureAppConfigurationKeys.CacheExpiration), out var _);
public static bool UseCacheExpiration =>
EnvironmentVariable.HasValue(AzureAppConfigurationKeys.CacheExpiration)
&& double.TryParse(EnvironmentVariable.GetValue(AzureAppConfigurationKeys.CacheExpiration), out var _);

public static double CacheExpiration => double.Parse(EnvironmentVariable.GetValue(AzureAppConfigurationKeys.CacheExpiration)!);
public static double CacheExpiration => double.Parse(EnvironmentVariable.GetValue(AzureAppConfigurationKeys.CacheExpiration)!);

public static string? ConnectionString =>
EnvironmentVariable.GetValue(AzureAppConfigurationKeys.ConnectionString);
public static string? ConnectionString =>
EnvironmentVariable.GetValue(AzureAppConfigurationKeys.ConnectionString);

public static string? ManagedIdentityEndpoint =>
EnvironmentVariable.GetValue(AzureAppConfigurationKeys.ManagedIdentityEndpoint);
public static string? ManagedIdentityEndpoint =>
EnvironmentVariable.GetValue(AzureAppConfigurationKeys.ManagedIdentityEndpoint);

public static string? Label =>
EnvironmentVariable.GetValue(AzureAppConfigurationKeys.Label);
}
public static string? Label =>
EnvironmentVariable.GetValue(AzureAppConfigurationKeys.Label);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace HealthChecks.UI.Image.Configuration
namespace HealthChecks.UI.Image.Configuration;

public class AzureAppConfigurationKeys
{
public class AzureAppConfigurationKeys
{
public const string Enabled = "AAC_Enabled";
public const string ConnectionString = "AAC_ConnectionString";
public const string ManagedIdentityEndpoint = "AAC_ManagedIdentityEndpoint";
public const string Label = "AAC_Label";
public const string CacheExpiration = "AAC_CacheExpiration";
}
public const string Enabled = "AAC_Enabled";
public const string ConnectionString = "AAC_ConnectionString";
public const string ManagedIdentityEndpoint = "AAC_ManagedIdentityEndpoint";
public const string Label = "AAC_Label";
public const string CacheExpiration = "AAC_CacheExpiration";
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace HealthChecks.UI.Image.Configuration.Helpers
namespace HealthChecks.UI.Image.Configuration.Helpers;

public class EnvironmentVariable
{
public class EnvironmentVariable
{
public static string? GetValue(string variable) =>
Environment.GetEnvironmentVariable(variable);
public static string? GetValue(string variable) =>
Environment.GetEnvironmentVariable(variable);

public static bool HasValue(string variable) =>
!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(variable));
}
public static bool HasValue(string variable) =>
!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(variable));
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
namespace HealthChecks.UI.Image.Configuration
namespace HealthChecks.UI.Image.Configuration;

public class PushServiceKeys
{
public class PushServiceKeys
{
public const string Enabled = "enable_push_endpoint";
public const string PushEndpointSecret = "push_endpoint_secret";
public const int ServiceAdded = 0;
public const int ServiceUpdated = 1;
public const int ServiceRemoved = 2;
public const string AuthParameter = "key";
}
public const string Enabled = "enable_push_endpoint";
public const string PushEndpointSecret = "push_endpoint_secret";
public const int ServiceAdded = 0;
public const int ServiceUpdated = 1;
public const int ServiceRemoved = 2;
public const string AuthParameter = "key";
}
Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
namespace HealthChecks.UI.Image.Configuration
namespace HealthChecks.UI.Image.Configuration;

public class Storage
{
public class Storage
public static IDictionary<StorageProviderEnum, StorageProvider> GetProviders()
{
public static IDictionary<StorageProviderEnum, StorageProvider> GetProviders()
return new Dictionary<StorageProviderEnum, StorageProvider>
{
return new Dictionary<StorageProviderEnum, StorageProvider>
[StorageProviderEnum.InMemory] = new StorageProvider
{
SetupProvider = (builder, connection) => builder.AddInMemoryStorage(),
RequiresConnectionString = false
},
[StorageProviderEnum.SqlServer] = new StorageProvider
{
SetupProvider = (builder, connection) => builder.AddSqlServerStorage(connection),
RequiresConnectionString = true,
},
[StorageProviderEnum.Sqlite] = new StorageProvider
{
SetupProvider = (builder, connection) => builder.AddSqliteStorage(connection),
RequiresConnectionString = true,
},
[StorageProviderEnum.PostgreSql] = new StorageProvider
{
[StorageProviderEnum.InMemory] = new StorageProvider
{
SetupProvider = (builder, connection) => builder.AddInMemoryStorage(),
RequiresConnectionString = false
},
[StorageProviderEnum.SqlServer] = new StorageProvider
{
SetupProvider = (builder, connection) => builder.AddSqlServerStorage(connection),
RequiresConnectionString = true,
},
[StorageProviderEnum.Sqlite] = new StorageProvider
{
SetupProvider = (builder, connection) => builder.AddSqliteStorage(connection),
RequiresConnectionString = true,
},
[StorageProviderEnum.PostgreSql] = new StorageProvider
{
SetupProvider = (builder, connection) => builder.AddPostgreSqlStorage(connection),
RequiresConnectionString = true,
},
[StorageProviderEnum.MySql] = new StorageProvider
{
SetupProvider = (builder, connection) => builder.AddMySqlStorage(connection),
RequiresConnectionString = true,
}
};
}
SetupProvider = (builder, connection) => builder.AddPostgreSqlStorage(connection),
RequiresConnectionString = true,
},
[StorageProviderEnum.MySql] = new StorageProvider
{
SetupProvider = (builder, connection) => builder.AddMySqlStorage(connection),
RequiresConnectionString = true,
}
};
}
}

public class StorageProvider
{
public bool RequiresConnectionString { get; set; }
public Action<HealthChecksUIBuilder, string> SetupProvider { get; set; }
= (_, connection) => throw new InvalidOperationException($"Misconfigured storage provider for {connection}");
}
public class StorageProvider
{
public bool RequiresConnectionString { get; set; }
public Action<HealthChecksUIBuilder, string> SetupProvider { get; set; }
= (_, connection) => throw new InvalidOperationException($"Misconfigured storage provider for {connection}");
}

public enum StorageProviderEnum
{
InMemory = 0,
SqlServer = 1,
Sqlite = 2,
PostgreSql = 3,
MySql = 4
}
public enum StorageProviderEnum
{
InMemory = 0,
SqlServer = 1,
Sqlite = 2,
PostgreSql = 3,
MySql = 4
}
23 changes: 11 additions & 12 deletions build/docker-images/HealthChecks.UI.Image/Configuration/UIKeys.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
namespace HealthChecks.UI.Image.Configuration
namespace HealthChecks.UI.Image.Configuration;

public class UIKeys
{
public class UIKeys
{
public const string UI_STYLESHEET = "ui_stylesheet";
public const string UI_API_PATH = "ui_api_path";
public const string UI_PATH = "ui_path";
public const string UI_RESOURCES_PATH = "ui_resources_path";
public const string UI_WEBHOOKS_PATH = "ui_webhooks_path";
public const string UI_NO_RELATIVE_PATHS = "ui_no_relative_paths";
public const string STORAGE_PROVIDER = "storage_provider";
public const string STORAGE_CONNECTION = "storage_connection";
}
public const string UI_STYLESHEET = "ui_stylesheet";
public const string UI_API_PATH = "ui_api_path";
public const string UI_PATH = "ui_path";
public const string UI_RESOURCES_PATH = "ui_resources_path";
public const string UI_WEBHOOKS_PATH = "ui_webhooks_path";
public const string UI_NO_RELATIVE_PATHS = "ui_no_relative_paths";
public const string STORAGE_PROVIDER = "storage_provider";
public const string STORAGE_CONNECTION = "storage_connection";
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
using HealthChecks.UI.Image.Configuration;

namespace Microsoft.Extensions.DependencyInjection
namespace Microsoft.Extensions.DependencyInjection;

public static class HealthChecksUIBuilderExtensions
{
public static class HealthChecksUIBuilderExtensions
public static IServiceCollection AddStorageProvider(this HealthChecksUIBuilder builder, IConfiguration configuration)
{
public static IServiceCollection AddStorageProvider(this HealthChecksUIBuilder builder, IConfiguration configuration)
{
string? configuredStorage = configuration[UIKeys.STORAGE_PROVIDER];
string? connectionString = configuration[UIKeys.STORAGE_CONNECTION];

if (string.IsNullOrEmpty(configuredStorage))
configuredStorage = StorageProviderEnum.InMemory.ToString();
string? configuredStorage = configuration[UIKeys.STORAGE_PROVIDER];
string? connectionString = configuration[UIKeys.STORAGE_CONNECTION];

if (Enum.TryParse(configuredStorage, out StorageProviderEnum storageEnum))
{
var providers = Storage.GetProviders();

var targetProvider = providers[storageEnum];
if (string.IsNullOrEmpty(configuredStorage))
configuredStorage = StorageProviderEnum.InMemory.ToString();

if (targetProvider.RequiresConnectionString && string.IsNullOrEmpty(connectionString))
{
throw new ArgumentNullException($"{UIKeys.STORAGE_CONNECTION} value has not been configured and it's required for {storageEnum}");
}
if (Enum.TryParse(configuredStorage, out StorageProviderEnum storageEnum))
{
var providers = Storage.GetProviders();

Console.WriteLine($"Configuring image to work with {storageEnum} provider");
var targetProvider = providers[storageEnum];

targetProvider.SetupProvider(builder, connectionString!);
}
else
if (targetProvider.RequiresConnectionString && string.IsNullOrEmpty(connectionString))
{
throw new ArgumentException($"Variable {UIKeys.STORAGE_PROVIDER} has an invalid value: {configuredStorage}." +
$" Available providers are {string.Join(" , ", Enum.GetNames(typeof(StorageProviderEnum)))}");
throw new ArgumentNullException($"{UIKeys.STORAGE_CONNECTION} value has not been configured and it's required for {storageEnum}");
}

return builder.Services;
Console.WriteLine($"Configuring image to work with {storageEnum} provider");

targetProvider.SetupProvider(builder, connectionString!);
}
else
{
throw new ArgumentException($"Variable {UIKeys.STORAGE_PROVIDER} has an invalid value: {configuredStorage}." +
$" Available providers are {string.Join(" , ", Enum.GetNames(typeof(StorageProviderEnum)))}");
}

return builder.Services;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using HealthChecks.UI.Image.Configuration;

namespace HealthChecks.UI.Image.Extensions
namespace HealthChecks.UI.Image.Extensions;

public static class HttpRequestExtensions
{
public static class HttpRequestExtensions
public static bool IsAuthenticated(this HttpRequest request)
{
public static bool IsAuthenticated(this HttpRequest request)
{
return request.Query.ContainsKey(PushServiceKeys.AuthParameter) &&
request.Query[PushServiceKeys.AuthParameter] == Environment.GetEnvironmentVariable(PushServiceKeys.PushEndpointSecret);
}
return request.Query.ContainsKey(PushServiceKeys.AuthParameter) &&
request.Query[PushServiceKeys.AuthParameter] == Environment.GetEnvironmentVariable(PushServiceKeys.PushEndpointSecret);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,34 @@
using HealthChecks.UI.Image.Configuration;
using Microsoft.Extensions.Configuration.AzureAppConfiguration;

namespace HealthChecks.UI.Image.Extensions
namespace HealthChecks.UI.Image.Extensions;

public static class IConfigurationBuilderExtensions
{
public static class IConfigurationBuilderExtensions
public static IConfigurationBuilder UseAzureAppConfiguration(this IConfigurationBuilder builder)
{
public static IConfigurationBuilder UseAzureAppConfiguration(this IConfigurationBuilder builder)
Action<AzureAppConfigurationOptions> setupConfig = AzureAppConfiguration.UseConnectionString switch
{
Action<AzureAppConfigurationOptions> setupConfig = AzureAppConfiguration.UseConnectionString switch
{
true => options => options.Connect(AzureAppConfiguration.ConnectionString),
false => options => options.Connect(new Uri(AzureAppConfiguration.ManagedIdentityEndpoint!), new ManagedIdentityCredential())
};
true => options => options.Connect(AzureAppConfiguration.ConnectionString),
false => options => options.Connect(new Uri(AzureAppConfiguration.ManagedIdentityEndpoint!), new ManagedIdentityCredential())
};

builder.AddAzureAppConfiguration(options =>
{
setupConfig(options);
builder.AddAzureAppConfiguration(options =>
{
setupConfig(options);
if (AzureAppConfiguration.UseCacheExpiration)
{
options.ConfigureRefresh(config => config.SetCacheExpiration(TimeSpan.FromSeconds(AzureAppConfiguration.CacheExpiration)));
}
if (AzureAppConfiguration.UseCacheExpiration)
{
options.ConfigureRefresh(config => config.SetCacheExpiration(TimeSpan.FromSeconds(AzureAppConfiguration.CacheExpiration)));
}
if (AzureAppConfiguration.UseLabel)
{
options.Select(KeyFilter.Any, AzureAppConfiguration.Label);
};
if (AzureAppConfiguration.UseLabel)
{
options.Select(KeyFilter.Any, AzureAppConfiguration.Label);
};
});
});

return builder;
}
return builder;
}
}
Loading

0 comments on commit dcfb0b7

Please sign in to comment.