ServiceCollectionExtensions.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.Reflection;
  2. using Microsoft.Extensions.Configuration;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using RackPeek.Domain.Git;
  5. using RackPeek.Domain.Persistence;
  6. using RackPeek.Domain.Resources;
  7. using RackPeek.Domain.Resources.Connections;
  8. using RackPeek.Domain.Resources.Hardware;
  9. using RackPeek.Domain.Resources.Services;
  10. using RackPeek.Domain.Resources.SystemResources;
  11. using RackPeek.Domain.UseCases;
  12. using RackPeek.Domain.UseCases.Cpus;
  13. using RackPeek.Domain.UseCases.Drives;
  14. using RackPeek.Domain.UseCases.Gpus;
  15. using RackPeek.Domain.UseCases.Labels;
  16. using RackPeek.Domain.UseCases.Ports;
  17. using RackPeek.Domain.UseCases.Tags;
  18. namespace RackPeek.Domain;
  19. public interface IResourceUseCase<T> where T : Resource {
  20. }
  21. public static class ServiceCollectionExtensions {
  22. public static IServiceCollection AddGitServices(
  23. this IServiceCollection services,
  24. IConfiguration config,
  25. string? yamlPath = null) {
  26. var gitToken = config["GIT_TOKEN"];
  27. if (!string.IsNullOrEmpty(gitToken) && !string.IsNullOrWhiteSpace(yamlPath)) {
  28. var gitUsername = config["GIT_USERNAME"] ?? "git";
  29. services.AddSingleton<IGitCredentialsProvider>(
  30. _ => new GitHubTokenCredentialsProvider(gitUsername, gitToken));
  31. services.AddSingleton<IGitRepository>(sp => {
  32. IGitCredentialsProvider creds = sp.GetRequiredService<IGitCredentialsProvider>();
  33. return new LibGit2GitRepository(yamlPath, creds);
  34. });
  35. RpkConstants.HasGitServices = true;
  36. }
  37. else {
  38. RpkConstants.HasGitServices = false;
  39. services.AddSingleton<IGitRepository, NullGitRepository>();
  40. }
  41. return services;
  42. }
  43. public static IServiceCollection AddResourceUseCases(
  44. this IServiceCollection services,
  45. Assembly assembly) {
  46. IEnumerable<Type> types = assembly.GetTypes()
  47. .Where(t => !t.IsAbstract && !t.IsInterface);
  48. foreach (Type type in types) {
  49. IEnumerable<Type> resourceUseCaseInterfaces = type.GetInterfaces()
  50. .Where(i =>
  51. i.IsGenericType &&
  52. i.GetInterfaces().Any(parent =>
  53. parent.IsGenericType &&
  54. parent.GetGenericTypeDefinition() == typeof(IResourceUseCase<>)));
  55. foreach (Type serviceType in resourceUseCaseInterfaces) services.AddScoped(serviceType, type);
  56. }
  57. return services;
  58. }
  59. public static IServiceCollection AddUseCases(
  60. this IServiceCollection services) {
  61. services.AddScoped(typeof(IAddResourceUseCase<>), typeof(AddResourceUseCase<>));
  62. services.AddScoped(typeof(IAddLabelUseCase<>), typeof(AddLabelUseCase<>));
  63. services.AddScoped(typeof(IAddTagUseCase<>), typeof(AddTagUseCase<>));
  64. services.AddScoped(typeof(ICloneResourceUseCase<>), typeof(CloneResourceUseCase<>));
  65. services.AddScoped(typeof(IDeleteResourceUseCase<>), typeof(DeleteResourceUseCase<>));
  66. services.AddScoped(typeof(IRemoveLabelUseCase<>), typeof(RemoveLabelUseCase<>));
  67. services.AddScoped(typeof(IRemoveTagUseCase<>), typeof(RemoveTagUseCase<>));
  68. services.AddScoped(typeof(IGetAllResourcesByKindUseCase<>), typeof(GetAllResourcesByKindUseCase<>));
  69. services.AddScoped(typeof(IGetResourceByNameUseCase<>), typeof(GetResourceByNameUseCase<>));
  70. services.AddScoped(typeof(IRenameResourceUseCase<>), typeof(RenameResourceUseCase<>));
  71. services.AddScoped(typeof(IAddCpuUseCase<>), typeof(AddCpuUseCase<>));
  72. services.AddScoped(typeof(IRemoveCpuUseCase<>), typeof(RemoveCpuUseCase<>));
  73. services.AddScoped(typeof(IUpdateCpuUseCase<>), typeof(UpdateCpuUseCase<>));
  74. services.AddScoped(typeof(IAddDriveUseCase<>), typeof(AddDriveUseCase<>));
  75. services.AddScoped(typeof(IRemoveDriveUseCase<>), typeof(RemoveDriveUseCase<>));
  76. services.AddScoped(typeof(IUpdateDriveUseCase<>), typeof(UpdateDriveUseCase<>));
  77. services.AddScoped(typeof(IAddGpuUseCase<>), typeof(AddGpuUseCase<>));
  78. services.AddScoped(typeof(IRemoveGpuUseCase<>), typeof(RemoveGpuUseCase<>));
  79. services.AddScoped(typeof(IUpdateGpuUseCase<>), typeof(UpdateGpuUseCase<>));
  80. services.AddScoped(typeof(IAddPortUseCase<>), typeof(AddPortUseCase<>));
  81. services.AddScoped(typeof(IRemovePortUseCase<>), typeof(RemovePortUseCase<>));
  82. services.AddScoped(typeof(IUpdatePortUseCase<>), typeof(UpdatePortUseCase<>));
  83. services.AddScoped(typeof(IAddConnectionUseCase), typeof(AddConnectionUseCase));
  84. services.AddScoped(typeof(IGetConnectionForPortUseCase), typeof(GetConnectionForPortUseCase));
  85. services.AddScoped(typeof(IGetConnectionsForResourceUseCase), typeof(GetConnectionsForResourceUseCase));
  86. services.AddScoped(typeof(IRemoveConnectionUseCase), typeof(RemoveConnectionUseCase));
  87. IEnumerable<Type>? usecases = Assembly.GetAssembly(typeof(IUseCase))
  88. ?.GetTypes()
  89. .Where(t =>
  90. !t.IsAbstract &&
  91. typeof(IUseCase).IsAssignableFrom(t)
  92. );
  93. if (usecases != null)
  94. foreach (Type type in usecases)
  95. services.AddScoped(type);
  96. return services;
  97. }
  98. public static IServiceCollection AddYamlRepos(
  99. this IServiceCollection services) {
  100. services.AddScoped<IHardwareRepository, YamlHardwareRepository>();
  101. services.AddScoped<ISystemRepository, YamlSystemRepository>();
  102. services.AddScoped<IServiceRepository, ServiceRepository>();
  103. return services;
  104. }
  105. }