Skip to content

Commit

Permalink
refactor: refactoring with Resharper
Browse files Browse the repository at this point in the history
Signed-off-by: 陳鈞 <[email protected]>
  • Loading branch information
jim60105 committed Apr 24, 2024
1 parent c99ca02 commit d199e5b
Show file tree
Hide file tree
Showing 74 changed files with 1,833 additions and 1,608 deletions.
75 changes: 20 additions & 55 deletions .editorconfig

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions DependencyInjection/AddAzureBlobStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static IServiceCollection AddAzureBlobStorageService(this IServiceCollect
{
try
{
var azureOptions = services.BuildServiceProvider().GetRequiredService<IOptions<AzureOption>>().Value;
AzureOption azureOptions = services.BuildServiceProvider().GetRequiredService<IOptions<AzureOption>>().Value;
if (null == azureOptions.BlobStorage
|| string.IsNullOrEmpty(azureOptions.BlobStorage.StorageAccountName)
|| string.IsNullOrEmpty(azureOptions.BlobStorage.StorageAccountKey)
Expand All @@ -25,7 +25,7 @@ public static IServiceCollection AddAzureBlobStorageService(this IServiceCollect
services.AddAzureClients(clientsBuilder
=> clientsBuilder.AddBlobServiceClient(azureOptions.BlobStorage.ConnectionString));

services.AddSingleton<IStorageService, ABSService>();
services.AddSingleton<IStorageService, AbsService>();

return services;
}
Expand Down
18 changes: 10 additions & 8 deletions DependencyInjection/AddAzureContainerInstanceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,28 @@ public static IServiceCollection AddAzureContainerInstanceService(this IServiceC
{
try
{
var azureOptions = services.BuildServiceProvider().GetRequiredService<IOptions<AzureOption>>().Value;
AzureOption azureOptions = services.BuildServiceProvider().GetRequiredService<IOptions<AzureOption>>().Value;
if (null == azureOptions.ContainerInstance
|| string.IsNullOrEmpty(azureOptions.ContainerInstance.ClientSecret.ClientID)
|| string.IsNullOrEmpty(azureOptions.ContainerInstance.ClientSecret.ClientSecret))
throw new ConfigurationErrorsException();

services.AddAzureClients(clientsBuilder
=> clientsBuilder.UseCredential((options)
=> new ClientSecretCredential(tenantId: azureOptions.ContainerInstance.ClientSecret.TenantID,
clientId: azureOptions.ContainerInstance.ClientSecret.ClientID,
clientSecret: azureOptions.ContainerInstance.ClientSecret.ClientSecret))
.AddClient<ArmClient, ArmClientOptions>((options, token) => new ArmClient(token)));
=> clientsBuilder.UseCredential((_)
=> new ClientSecretCredential(
tenantId: azureOptions.ContainerInstance.ClientSecret.TenantID,
clientId: azureOptions.ContainerInstance.ClientSecret.ClientID,
clientSecret: azureOptions.ContainerInstance.ClientSecret
.ClientSecret))
.AddClient<ArmClient, ArmClientOptions>((_, token) => new ArmClient(token)));

services.AddSingleton<IJobService, ACIService>();
services.AddSingleton<IJobService, AciService>();

services.AddSingleton<IYtarchiveService, YtarchiveService>();
services.AddSingleton<IYtdlpService, YtdlpService>();
services.AddSingleton<IStreamlinkService, StreamlinkService>();
services.AddSingleton<ITwitcastingRecorderService, TwitcastingRecorderService>();
services.AddSingleton<IFC2LiveDLService, FC2LiveDLService>();
services.AddSingleton<IFc2LiveDLService, Fc2LiveDLService>();

services.AddSingleton<IAzureUploaderService, AzureUploaderService>();
services.AddSingleton<IS3UploaderService, S3UploaderService>();
Expand Down
11 changes: 6 additions & 5 deletions DependencyInjection/AddCosmosDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace LivestreamRecorderService.DependencyInjection;

public static partial class Extensions
{
public static IServiceCollection AddCosmosDB(this IServiceCollection services, IConfiguration configuration)
public static IServiceCollection AddCosmosDb(this IServiceCollection services, IConfiguration configuration)
{
#if !COSMOSDB
Log.Fatal("This is a CouchDB build. Please use the CosmosDB build for Azure CosmosDB support.");
Expand All @@ -36,18 +36,19 @@ public static IServiceCollection AddCosmosDB(this IServiceCollection services, I
.EnableSensitiveDataLogging()
#endif
.UseCosmos(connectionString: azureOptions.CosmosDB.Public.ConnectionStrings,
databaseName: azureOptions.CosmosDB.Public.DatabaseName,
cosmosOptionsAction: option => option.GatewayModeMaxConnectionLimit(380));
databaseName: azureOptions.CosmosDB.Public.DatabaseName,
cosmosOptionsAction: option => option.GatewayModeMaxConnectionLimit(380));
});

services.AddDbContext<PrivateContext>((options) =>
{
options
#if !RELEASE
.EnableSensitiveDataLogging()
#endif
.UseCosmos(connectionString: azureOptions.CosmosDB.Private.ConnectionStrings,
databaseName: azureOptions.CosmosDB.Private.DatabaseName,
cosmosOptionsAction: option => option.GatewayModeMaxConnectionLimit(380));
databaseName: azureOptions.CosmosDB.Private.DatabaseName,
cosmosOptionsAction: option => option.GatewayModeMaxConnectionLimit(380));
});

services.AddScoped<UnitOfWork_Public>();
Expand Down
38 changes: 23 additions & 15 deletions DependencyInjection/AddCouchDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,60 @@
using LivestreamRecorderService.Models.Options;
using Microsoft.Extensions.Options;
using System.Configuration;
using Flurl.Http.Configuration;
#endif
using Serilog;
using Flurl.Http.Configuration;

namespace LivestreamRecorderService.DependencyInjection;

public static partial class Extensions
{
public static IServiceCollection AddCouchDB(this IServiceCollection services, IConfiguration configuration)
public static IServiceCollection AddCouchDb(this IServiceCollection services, IConfiguration configuration)
{
#if !COUCHDB
Log.Fatal("This is a CosmosDB build. Please use the CouchDB build for Apache CouchDB support.");
throw new InvalidOperationException("This is a CosmosDB build. Please use the CouchDB build for Apache CouchDB support.");
#else
try
{
var couchDBOptions = services.BuildServiceProvider().GetRequiredService<IOptions<CouchDBOption>>().Value;
CouchDbOption? couchDbOptions = services.BuildServiceProvider().GetRequiredService<IOptions<CouchDbOption>>().Value;

if (null == couchDBOptions
|| string.IsNullOrEmpty(couchDBOptions.Endpoint)
|| string.IsNullOrEmpty(couchDBOptions.Username)
|| string.IsNullOrEmpty(couchDBOptions.Password))
if (null == couchDbOptions
|| string.IsNullOrEmpty(couchDbOptions.Endpoint)
|| string.IsNullOrEmpty(couchDbOptions.Username)
|| string.IsNullOrEmpty(couchDbOptions.Password))
throw new ConfigurationErrorsException();

services.AddSingleton<CouchDBHttpClientFactory>();
services.AddSingleton<CouchDbHttpClientFactory>();

// https://github.com/matteobortolazzo/couchdb-net#dependency-injection
services.AddCouchContext<CouchDBContext>((options) =>
{
options
.UseEndpoint(couchDBOptions.Endpoint)
.UseCookieAuthentication(username: couchDBOptions.Username, password: couchDBOptions.Password)
.UseEndpoint(couchDbOptions.Endpoint)
.UseCookieAuthentication(username: couchDbOptions.Username, password: couchDbOptions.Password)
.ConfigureFlurlClient(setting =>
{
// Always use the same HttpClient instance to optimize resource usage and performance.
setting.HttpClientFactory = services.BuildServiceProvider().GetRequiredService<CouchDBHttpClientFactory>();
setting.HttpClientFactory = services.BuildServiceProvider().GetRequiredService<CouchDbHttpClientFactory>();
#if !RELEASE
setting.BeforeCall = call
=> Log.Debug("Sending request to couch: {request} {body}", call, call.RequestBody);

setting.AfterCallAsync = call => Task.Run(() =>
{
if (call.Succeeded)
{
Log.Debug("Received response from couch: {response} {body}", call, call.Response.ResponseMessage.Content.ReadAsStringAsync().Result);
Log.Debug("Received response from couch: {response} {body}",
call,
call.Response.ResponseMessage.Content.ReadAsStringAsync().Result);
}
});
#endif
setting.OnErrorAsync = call => Task.Run(()
=> Log.Error("Request Failed: {request} {body}", call, call?.RequestBody ?? string.Empty));
=> Log.Error("Request Failed: {request} {body}",
call,
call?.RequestBody ?? string.Empty));
})
.SetPropertyCase(PropertyCaseType.None);
});
Expand All @@ -73,22 +78,25 @@ public static IServiceCollection AddCouchDB(this IServiceCollection services, IC
catch (ConfigurationErrorsException)
{
Log.Fatal("Missing CouchDB Settings. Please set CouchDB:Endpoint CouchDB:Username CouchDB:Password in appsettings.json.");
throw new ConfigurationErrorsException("Missing CouchDB Settings. Please set CouchDB:Endpoint CouchDB:Username CouchDB:Password in appsettings.json.");
throw new ConfigurationErrorsException(
"Missing CouchDB Settings. Please set CouchDB:Endpoint CouchDB:Username CouchDB:Password in appsettings.json.");
}
#endif
}

#if COUCHDB
/// <summary>
/// Custom HTTP client factory for CouchDB interactions.
/// </summary>
/// <remarks>
/// This factory provides a globally shared instance of HttpClient to optimize resource usage and performance.
/// </remarks>
private class CouchDBHttpClientFactory : DefaultHttpClientFactory
private class CouchDbHttpClientFactory : DefaultHttpClientFactory
{
private static HttpClient? _httpClient;

public override HttpClient CreateHttpClient(HttpMessageHandler handler)
=> _httpClient ??= base.CreateHttpClient(handler);
}
#endif
}
15 changes: 9 additions & 6 deletions DependencyInjection/AddDiscordService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using LivestreamRecorderService.Models.OptionDiscords;
using LivestreamRecorderService.SingletonServices;
using LivestreamRecorderService.SingletonServices;
using Serilog;
using System.Configuration;
using LivestreamRecorderService.Models.Options;

namespace LivestreamRecorderService.DependencyInjection;

Expand All @@ -12,7 +12,7 @@ public static IServiceCollection AddDiscordService(this IServiceCollection servi
try
{
IConfigurationSection config = configuration.GetSection(DiscordOption.ConfigurationSectionName);
var discordOptions = config.Get<DiscordOption>();
DiscordOption? discordOptions = config.Get<DiscordOption>();
if (null == discordOptions) throw new ConfigurationErrorsException();

services.AddOptions<DiscordOption>()
Expand All @@ -31,15 +31,18 @@ public static IServiceCollection AddDiscordService(this IServiceCollection servi
|| string.IsNullOrEmpty(discordOptions.FrontEndHost)
|| null == discordOptions.Mention
|| null == discordOptions.Emotes
) throw new ConfigurationErrorsException();
) throw new ConfigurationErrorsException();

services.AddSingleton<DiscordService>();
return services;
}
catch (ConfigurationErrorsException)
{
Log.Fatal("Missing Discord Settings. Please set Discord:Enabled Discord:Webhook, Discord:WebhookWarning, Discord:WebhookAdmin, Discord:FrontEndHost, Discord:Mention and Discord:Emotes in appsettings.json.");
throw new ConfigurationErrorsException("Missing Discord Settings. Please set Discord:Enabled Discord:Webhook, Discord:WebhookWarning, Discord:WebhookAdmin, Discord:FrontEndHost, Discord:Mention and Discord:Emotes in appsettings.json.");
Log.Fatal(
"Missing Discord Settings. Please set Discord:Enabled Discord:Webhook, Discord:WebhookWarning, Discord:WebhookAdmin, Discord:FrontEndHost, Discord:Mention and Discord:Emotes in appsettings.json.");

throw new ConfigurationErrorsException(
"Missing Discord Settings. Please set Discord:Enabled Discord:Webhook, Discord:WebhookWarning, Discord:WebhookAdmin, Discord:FrontEndHost, Discord:Mention and Discord:Emotes in appsettings.json.");
}
}
}
5 changes: 3 additions & 2 deletions DependencyInjection/AddHeartbeatWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static IServiceCollection AddHeartbeatWorker(this IServiceCollection serv
try
{
IConfigurationSection config = configuration.GetSection(HeartbeatOption.ConfigurationSectionName);
var heartbeatOptions = config.Get<HeartbeatOption>();
HeartbeatOption? heartbeatOptions = config.Get<HeartbeatOption>();
if (null == heartbeatOptions) throw new ConfigurationErrorsException();

services.AddOptions<HeartbeatOption>()
Expand All @@ -31,7 +31,8 @@ public static IServiceCollection AddHeartbeatWorker(this IServiceCollection serv
catch (ConfigurationErrorsException)
{
Log.Fatal("Missing Twitch ClientId or ClientSecret. Please set Twitch:ClientId and Twitch:ClientSecret in appsettings.json.");
throw new ConfigurationErrorsException("Missing Twitch ClientId or ClientSecret. Please set Twitch:ClientId and Twitch:ClientSecret in appsettings.json.");
throw new ConfigurationErrorsException(
"Missing Twitch ClientId or ClientSecret. Please set Twitch:ClientId and Twitch:ClientSecret in appsettings.json.");
}
}
}
25 changes: 13 additions & 12 deletions DependencyInjection/AddKubernetesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,44 @@ public static IServiceCollection AddKubernetesService(this IServiceCollection se
{
try
{
var serviceOptions = services.BuildServiceProvider().GetRequiredService<IOptions<ServiceOption>>().Value;
ServiceOption serviceOptions = services.BuildServiceProvider().GetRequiredService<IOptions<ServiceOption>>().Value;

if (serviceOptions.JobService != ServiceName.Kubernetes)
{
return services;
}

IConfigurationSection config = configuration.GetSection(KubernetesOption.ConfigurationSectionName);
var kubernetesOptions = config.Get<KubernetesOption>();
KubernetesOption? kubernetesOptions = config.Get<KubernetesOption>();
if (null == kubernetesOptions) throw new ConfigurationErrorsException();

services.AddOptions<KubernetesOption>()
.Bind(config)
.ValidateDataAnnotations()
.ValidateOnStart();

KubernetesClientConfiguration k8sConfig =
KubernetesClientConfiguration k8SConfig =
// skipcq: CS-R1114
kubernetesOptions.UseTheSameCluster
? KubernetesClientConfiguration.InClusterConfig()
: !string.IsNullOrWhiteSpace(kubernetesOptions.ConfigFile)
? KubernetesClientConfiguration.BuildConfigFromConfigFile(kubernetesOptions.ConfigFile)
: KubernetesClientConfiguration.BuildDefaultConfig();
var client = new Kubernetes(k8sConfig);
services.AddSingleton<Kubernetes>(client);
? KubernetesClientConfiguration.InClusterConfig()
: !string.IsNullOrWhiteSpace(kubernetesOptions.ConfigFile)
? KubernetesClientConfiguration.BuildConfigFromConfigFile(kubernetesOptions.ConfigFile)
: KubernetesClientConfiguration.BuildDefaultConfig();

var client = new Kubernetes(k8SConfig);
services.AddSingleton(client);

services.AddSingleton<IJobService, KubernetesService>();

KubernetesService.KubernetesNamespace = string.IsNullOrWhiteSpace(kubernetesOptions.Namespace)
? "recordermoe"
: kubernetesOptions.Namespace;
? "recordermoe"
: kubernetesOptions.Namespace;

services.AddSingleton<IYtarchiveService, YtarchiveService>();
services.AddSingleton<IYtdlpService, YtdlpService>();
services.AddSingleton<IStreamlinkService, StreamlinkService>();
services.AddSingleton<ITwitcastingRecorderService, TwitcastingRecorderService>();
services.AddSingleton<IFC2LiveDLService, FC2LiveDLService>();
services.AddSingleton<IFc2LiveDLService, Fc2LiveDLService>();

services.AddSingleton<IAzureUploaderService, AzureUploaderService>();
services.AddSingleton<IS3UploaderService, S3UploaderService>();
Expand Down
10 changes: 5 additions & 5 deletions DependencyInjection/AddS3StorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ public static IServiceCollection AddS3StorageService(this IServiceCollection ser
{
try
{
var s3Options = services.BuildServiceProvider().GetRequiredService<IOptions<S3Option>>().Value;
S3Option s3Options = services.BuildServiceProvider().GetRequiredService<IOptions<S3Option>>().Value;
if (string.IsNullOrEmpty(s3Options.Endpoint)
|| string.IsNullOrEmpty(s3Options.AccessKey)
|| string.IsNullOrEmpty(s3Options.SecretKey)
|| string.IsNullOrEmpty(s3Options.BucketName_Public)
|| string.IsNullOrEmpty(s3Options.BucketName_Private))
throw new ConfigurationErrorsException();

var minio = new MinioClient().WithEndpoint(s3Options.Endpoint)
.WithCredentials(s3Options.AccessKey, s3Options.SecretKey)
.WithSSL(s3Options.Secure)
.Build();
IMinioClient? minio = new MinioClient().WithEndpoint(s3Options.Endpoint)
.WithCredentials(s3Options.AccessKey, s3Options.SecretKey)
.WithSSL(s3Options.Secure)
.Build();

services.AddSingleton(minio);
services.AddSingleton<IStorageService, S3Service>();
Expand Down
Loading

0 comments on commit d199e5b

Please sign in to comment.