Skip to content

Commit

Permalink
refactor: fix DeepSource advices
Browse files Browse the repository at this point in the history
Signed-off-by: 陳鈞 <[email protected]>
  • Loading branch information
jim60105 committed Mar 6, 2024
1 parent 9fa053c commit 2a8e057
Show file tree
Hide file tree
Showing 41 changed files with 343 additions and 326 deletions.
43 changes: 21 additions & 22 deletions DependencyInjection/AddAzureBlobStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,33 @@
using Serilog;
using System.Configuration;

namespace LivestreamRecorderService.DependencyInjection
namespace LivestreamRecorderService.DependencyInjection;

public static partial class Extensions
{
public static partial class Extensions
public static IServiceCollection AddAzureBlobStorageService(this IServiceCollection services)
{
public static IServiceCollection AddAzureBlobStorageService(this IServiceCollection services)
try
{
try
{
var azureOptions = services.BuildServiceProvider().GetRequiredService<IOptions<AzureOption>>().Value;
if (null == azureOptions.BlobStorage
|| string.IsNullOrEmpty(azureOptions.BlobStorage.StorageAccountName)
|| string.IsNullOrEmpty(azureOptions.BlobStorage.StorageAccountKey)
|| string.IsNullOrEmpty(azureOptions.BlobStorage.BlobContainerName_Public)
|| string.IsNullOrEmpty(azureOptions.BlobStorage.BlobContainerName_Private))
throw new ConfigurationErrorsException();
var azureOptions = services.BuildServiceProvider().GetRequiredService<IOptions<AzureOption>>().Value;
if (null == azureOptions.BlobStorage
|| string.IsNullOrEmpty(azureOptions.BlobStorage.StorageAccountName)
|| string.IsNullOrEmpty(azureOptions.BlobStorage.StorageAccountKey)
|| string.IsNullOrEmpty(azureOptions.BlobStorage.BlobContainerName_Public)
|| string.IsNullOrEmpty(azureOptions.BlobStorage.BlobContainerName_Private))
throw new ConfigurationErrorsException();

services.AddAzureClients(clientsBuilder
=> clientsBuilder.AddBlobServiceClient(azureOptions.BlobStorage.ConnectionString));
services.AddAzureClients(clientsBuilder
=> clientsBuilder.AddBlobServiceClient(azureOptions.BlobStorage.ConnectionString));

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

return services;
}
catch (ConfigurationErrorsException)
{
Log.Fatal("Missing AzuerBlobStorage. Please set Azure:AzuerBlobStorage in appsettings.json.");
throw new ConfigurationErrorsException("Missing AzuerBlobStorage. Please set Azure:AzuerBlobStorage in appsettings.json.");
}
return services;
}
catch (ConfigurationErrorsException)
{
Log.Fatal("Missing AzuerBlobStorage. Please set Azure:AzuerBlobStorage in appsettings.json.");
throw new ConfigurationErrorsException("Missing AzuerBlobStorage. Please set Azure:AzuerBlobStorage in appsettings.json.");
}
}
}
61 changes: 30 additions & 31 deletions DependencyInjection/AddAzureContainerInstanceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,44 @@
using Serilog;
using System.Configuration;

namespace LivestreamRecorderService.DependencyInjection
namespace LivestreamRecorderService.DependencyInjection;

public static partial class Extensions
{
public static partial class Extensions
public static IServiceCollection AddAzureContainerInstanceService(this IServiceCollection services)
{
public static IServiceCollection AddAzureContainerInstanceService(this IServiceCollection services)
try
{
try
{
var 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();
var 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)));
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)));

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<IYtarchiveService, YtarchiveService>();
services.AddSingleton<IYtdlpService, YtdlpService>();
services.AddSingleton<IStreamlinkService, StreamlinkService>();
services.AddSingleton<ITwitcastingRecorderService, TwitcastingRecorderService>();
services.AddSingleton<IFC2LiveDLService, FC2LiveDLService>();

services.AddSingleton<IAzureUploaderService, AzureUploaderService>();
services.AddSingleton<IS3UploaderService, S3UploaderService>();
services.AddSingleton<IAzureUploaderService, AzureUploaderService>();
services.AddSingleton<IS3UploaderService, S3UploaderService>();

return services;
}
catch (ConfigurationErrorsException)
{
Log.Fatal("Missing AzureContainerInstance. Please set Azure:AzureContainerInstance in appsettings.json.");
throw new ConfigurationErrorsException("Missing AzureContainerInstance. Please set Azure:AzureContainerInstance in appsettings.json.");
}
return services;
}
catch (ConfigurationErrorsException)
{
Log.Fatal("Missing AzureContainerInstance. Please set Azure:AzureContainerInstance in appsettings.json.");
throw new ConfigurationErrorsException("Missing AzureContainerInstance. Please set Azure:AzureContainerInstance in appsettings.json.");
}
}
}
85 changes: 42 additions & 43 deletions DependencyInjection/AddCosmosDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,59 @@
#endif
using Serilog;

namespace LivestreamRecorderService.DependencyInjection
namespace LivestreamRecorderService.DependencyInjection;

public static partial class Extensions
{
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.");
throw new InvalidOperationException("This is a CouchDB build. Please use the CosmosDB build for Azure CosmosDB support.");
Log.Fatal("This is a CouchDB build. Please use the CosmosDB build for Azure CosmosDB support.");
throw new InvalidOperationException("This is a CouchDB build. Please use the CosmosDB build for Azure CosmosDB support.");
#else
try
{
var azureOptions = services.BuildServiceProvider().GetRequiredService<IOptions<AzureOption>>().Value;
try
{
var azureOptions = services.BuildServiceProvider().GetRequiredService<IOptions<AzureOption>>().Value;

if (null == azureOptions.CosmosDB
|| null == azureOptions.CosmosDB.Public.ConnectionStrings
|| null == azureOptions.CosmosDB.Private.ConnectionStrings)
throw new ConfigurationErrorsException();
if (null == azureOptions.CosmosDB
|| null == azureOptions.CosmosDB.Public.ConnectionStrings
|| null == azureOptions.CosmosDB.Private.ConnectionStrings)
throw new ConfigurationErrorsException();

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

services.AddScoped<UnitOfWork_Public>();
services.AddScoped<UnitOfWork_Private>();
services.AddScoped<IVideoRepository>((s) => new VideoRepository((IUnitOfWork)s.GetRequiredService(typeof(UnitOfWork_Public))));
services.AddScoped<IChannelRepository>((s) => new ChannelRepository((IUnitOfWork)s.GetRequiredService(typeof(UnitOfWork_Public))));
services.AddScoped<IUserRepository>((s) => new UserRepository((IUnitOfWork)s.GetRequiredService(typeof(UnitOfWork_Private))));
return services;
}
catch (ConfigurationErrorsException)
{
Log.Fatal("Missing CosmosDB Settings. Please setup CosmosDB in appsettings.json.");
throw new ConfigurationErrorsException("Missing CosmosDB Settings. Please setup CosmosDB in appsettings.json.");
}
#endif
services.AddScoped<UnitOfWork_Public>();
services.AddScoped<UnitOfWork_Private>();
services.AddScoped<IVideoRepository>((s) => new VideoRepository((IUnitOfWork)s.GetRequiredService(typeof(UnitOfWork_Public))));
services.AddScoped<IChannelRepository>((s) => new ChannelRepository((IUnitOfWork)s.GetRequiredService(typeof(UnitOfWork_Public))));
services.AddScoped<IUserRepository>((s) => new UserRepository((IUnitOfWork)s.GetRequiredService(typeof(UnitOfWork_Private))));
return services;
}
catch (ConfigurationErrorsException)
{
Log.Fatal("Missing CosmosDB Settings. Please setup CosmosDB in appsettings.json.");
throw new ConfigurationErrorsException("Missing CosmosDB Settings. Please setup CosmosDB in appsettings.json.");
}
#endif
}
}
61 changes: 30 additions & 31 deletions DependencyInjection/AddDiscordService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,43 @@
using Serilog;
using System.Configuration;

namespace LivestreamRecorderService.DependencyInjection
namespace LivestreamRecorderService.DependencyInjection;

public static partial class Extensions
{
public static partial class Extensions
public static IServiceCollection AddDiscordService(this IServiceCollection services, IConfiguration configuration)
{
public static IServiceCollection AddDiscordService(this IServiceCollection services, IConfiguration configuration)
try
{
try
{
IConfigurationSection config = configuration.GetSection(DiscordOption.ConfigurationSectionName);
var discordOptions = config.Get<DiscordOption>();
if (null == discordOptions) throw new ConfigurationErrorsException();

services.AddOptions<DiscordOption>()
.Bind(config)
.ValidateDataAnnotations()
.ValidateOnStart();
IConfigurationSection config = configuration.GetSection(DiscordOption.ConfigurationSectionName);
var discordOptions = config.Get<DiscordOption>();
if (null == discordOptions) throw new ConfigurationErrorsException();

if (!discordOptions.Enabled)
{
return services;
}
services.AddOptions<DiscordOption>()
.Bind(config)
.ValidateDataAnnotations()
.ValidateOnStart();

if (string.IsNullOrEmpty(discordOptions.Webhook)
|| string.IsNullOrEmpty(discordOptions.WebhookWarning)
|| string.IsNullOrEmpty(discordOptions.WebhookAdmin)
|| string.IsNullOrEmpty(discordOptions.FrontEndHost)
|| null == discordOptions.Mention
|| null == discordOptions.Emotes
) throw new ConfigurationErrorsException();

services.AddSingleton<DiscordService>();
return services;
}
catch (ConfigurationErrorsException)
if (!discordOptions.Enabled)
{
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.");
return services;
}

if (string.IsNullOrEmpty(discordOptions.Webhook)
|| string.IsNullOrEmpty(discordOptions.WebhookWarning)
|| string.IsNullOrEmpty(discordOptions.WebhookAdmin)
|| string.IsNullOrEmpty(discordOptions.FrontEndHost)
|| null == discordOptions.Mention
|| null == discordOptions.Emotes
) 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.");
}
}
}
45 changes: 22 additions & 23 deletions DependencyInjection/AddHeartbeatWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,35 @@
using Serilog;
using System.Configuration;

namespace LivestreamRecorderService.DependencyInjection
namespace LivestreamRecorderService.DependencyInjection;

public static partial class Extensions
{
public static partial class Extensions
public static IServiceCollection AddHeartbeatWorker(this IServiceCollection services, IConfiguration configuration)
{
public static IServiceCollection AddHeartbeatWorker(this IServiceCollection services, IConfiguration configuration)
try
{
try
{
IConfigurationSection config = configuration.GetSection(HeartbeatOption.ConfigurationSectionName);
var heartbeatOptions = config.Get<HeartbeatOption>();
if (null == heartbeatOptions) throw new ConfigurationErrorsException();

services.AddOptions<HeartbeatOption>()
.Bind(config)
.ValidateDataAnnotations()
.ValidateOnStart();
IConfigurationSection config = configuration.GetSection(HeartbeatOption.ConfigurationSectionName);
var heartbeatOptions = config.Get<HeartbeatOption>();
if (null == heartbeatOptions) throw new ConfigurationErrorsException();

if (!heartbeatOptions.Enabled)
{
return services;
}
services.AddOptions<HeartbeatOption>()
.Bind(config)
.ValidateDataAnnotations()
.ValidateOnStart();

services.AddHostedService<HeartbeatWorker>();
return services;
}
catch (ConfigurationErrorsException)
if (!heartbeatOptions.Enabled)
{
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.");
return services;
}

services.AddHostedService<HeartbeatWorker>();
return services;
}
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.");
}
}
}
Loading

0 comments on commit 2a8e057

Please sign in to comment.