Move GitHubService initialization to ServiceCollectionExtensions and make modifiable

This commit is contained in:
Dominion
2023-06-04 11:57:04 -04:00
parent 63beb10e4e
commit 2751abc44d
2 changed files with 36 additions and 4 deletions
@@ -51,7 +51,6 @@ using Tgstation.Server.Host.Swarm;
using Tgstation.Server.Host.System;
using Tgstation.Server.Host.Transfer;
using Tgstation.Server.Host.Utils;
using Tgstation.Server.Host.Utils.GitHub;
namespace Tgstation.Server.Host.Core
{
@@ -372,9 +371,7 @@ namespace Tgstation.Server.Host.Core
services.AddSingleton<IServerPortProvider, ServerPortProivder>();
services.AddSingleton<ITopicClientFactory, TopicClientFactory>();
services.AddSingleton<IGitHubClientFactory, GitHubClientFactory>();
services.AddSingleton<IGitHubServiceFactory, GitHubServiceFactory>();
services.AddSingleton(x => x.GetRequiredService<IGitHubServiceFactory>().CreateService());
services.AddGitHub();
// configure root services
services.AddSingleton<IJobService, JobService>();
@@ -15,6 +15,7 @@ using Serilog.Sinks.Elasticsearch;
using Tgstation.Server.Host.Components.Chat.Providers;
using Tgstation.Server.Host.Configuration;
using Tgstation.Server.Host.Utils;
using Tgstation.Server.Host.Utils.GitHub;
namespace Tgstation.Server.Host.Extensions
{
@@ -28,6 +29,11 @@ namespace Tgstation.Server.Host.Extensions
/// </summary>
static Type chatProviderFactoryType = typeof(ProviderFactory);
/// <summary>
/// The <see cref="IGitHubServiceFactory"/> implementation used in calls to <see cref="AddGitHub(IServiceCollection)"/>.
/// </summary>
static Type gitHubServiceFactoryType = typeof(GitHubServiceFactory);
/// <summary>
/// A <see cref="ServiceDescriptor"/> for an additional <see cref="ILoggerProvider"/> to use.
/// </summary>
@@ -42,6 +48,32 @@ namespace Tgstation.Server.Host.Extensions
chatProviderFactoryType = typeof(TProviderFactory);
}
/// <summary>
/// Change the <see cref="Type"/> used as an implementation for calls to <see cref="AddGitHub(IServiceCollection)"/>.
/// </summary>
/// <typeparam name="TGitHubServiceFactory">The <see cref="IGitHubServiceFactory"/> implementation to use.</typeparam>
public static void UseGitHubServiceFactory<TGitHubServiceFactory>() where TGitHubServiceFactory : IGitHubServiceFactory
{
gitHubServiceFactoryType = typeof(TGitHubServiceFactory);
}
/// <summary>
/// Adds a <see cref="IGitHubServiceFactory"/> implementation to the given <paramref name="serviceCollection"/>.
/// </summary>
/// <param name="serviceCollection">The <see cref="IServiceCollection"/> to configure.</param>
/// <returns><paramref name="serviceCollection"/>.</returns>
public static IServiceCollection AddGitHub(this IServiceCollection serviceCollection)
{
if (serviceCollection == null)
throw new ArgumentNullException(nameof(serviceCollection));
serviceCollection.AddSingleton<IGitHubClientFactory, GitHubClientFactory>();
serviceCollection.AddSingleton(typeof(IGitHubServiceFactory), gitHubServiceFactoryType);
serviceCollection.AddSingleton(x => x.GetRequiredService<IGitHubServiceFactory>().CreateService());
return serviceCollection;
}
/// <summary>
/// Add an additional <see cref="ILoggerProvider"/> to <see cref="IServiceCollection"/>s that call <see cref="SetupLogging(IServiceCollection, Action{LoggerConfiguration}, Action{LoggerSinkConfiguration}, ElasticsearchConfiguration)"/>.
/// </summary>
@@ -60,6 +92,9 @@ namespace Tgstation.Server.Host.Extensions
/// <returns><paramref name="serviceCollection"/>.</returns>
public static IServiceCollection AddChatProviderFactory(this IServiceCollection serviceCollection)
{
if (serviceCollection == null)
throw new ArgumentNullException(nameof(serviceCollection));
return serviceCollection.AddSingleton(typeof(IProviderFactory), chatProviderFactoryType);
}