ServiceCollectionExtensions.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. var insecureTls = string.Equals(
  30. config["GIT_INSECURE_TLS"], "true", StringComparison.OrdinalIgnoreCase);
  31. services.AddSingleton<IGitCredentialsProvider>(
  32. _ => new TokenCredentialsProvider(gitUsername, gitToken));
  33. services.AddSingleton<IGitRepository>(sp => {
  34. IGitCredentialsProvider creds = sp.GetRequiredService<IGitCredentialsProvider>();
  35. return new LibGit2GitRepository(yamlPath, creds, insecureTls);
  36. });
  37. RpkConstants.HasGitServices = true;
  38. }
  39. else {
  40. RpkConstants.HasGitServices = false;
  41. services.AddSingleton<IGitRepository, NullGitRepository>();
  42. }
  43. return services;
  44. }
  45. public static IServiceCollection AddResourceUseCases(
  46. this IServiceCollection services,
  47. Assembly assembly) {
  48. IEnumerable<Type> types = assembly.GetTypes()
  49. .Where(t => !t.IsAbstract && !t.IsInterface);
  50. foreach (Type type in types) {
  51. IEnumerable<Type> resourceUseCaseInterfaces = type.GetInterfaces()
  52. .Where(i =>
  53. i.IsGenericType &&
  54. i.GetInterfaces().Any(parent =>
  55. parent.IsGenericType &&
  56. parent.GetGenericTypeDefinition() == typeof(IResourceUseCase<>)));
  57. foreach (Type serviceType in resourceUseCaseInterfaces) services.AddScoped(serviceType, type);
  58. }
  59. return services;
  60. }
  61. public static IServiceCollection AddUseCases(
  62. this IServiceCollection services) {
  63. services.AddScoped(typeof(IAddResourceUseCase<>), typeof(AddResourceUseCase<>));
  64. services.AddScoped(typeof(IAddLabelUseCase<>), typeof(AddLabelUseCase<>));
  65. services.AddScoped(typeof(IAddTagUseCase<>), typeof(AddTagUseCase<>));
  66. services.AddScoped(typeof(ICloneResourceUseCase<>), typeof(CloneResourceUseCase<>));
  67. services.AddScoped(typeof(IDeleteResourceUseCase<>), typeof(DeleteResourceUseCase<>));
  68. services.AddScoped(typeof(IRemoveLabelUseCase<>), typeof(RemoveLabelUseCase<>));
  69. services.AddScoped(typeof(IRemoveTagUseCase<>), typeof(RemoveTagUseCase<>));
  70. services.AddScoped(typeof(IGetAllResourcesByKindUseCase<>), typeof(GetAllResourcesByKindUseCase<>));
  71. services.AddScoped(typeof(IGetResourceByNameUseCase<>), typeof(GetResourceByNameUseCase<>));
  72. services.AddScoped(typeof(IRenameResourceUseCase<>), typeof(RenameResourceUseCase<>));
  73. services.AddScoped(typeof(IAddCpuUseCase<>), typeof(AddCpuUseCase<>));
  74. services.AddScoped(typeof(IRemoveCpuUseCase<>), typeof(RemoveCpuUseCase<>));
  75. services.AddScoped(typeof(IUpdateCpuUseCase<>), typeof(UpdateCpuUseCase<>));
  76. services.AddScoped(typeof(IAddDriveUseCase<>), typeof(AddDriveUseCase<>));
  77. services.AddScoped(typeof(IRemoveDriveUseCase<>), typeof(RemoveDriveUseCase<>));
  78. services.AddScoped(typeof(IUpdateDriveUseCase<>), typeof(UpdateDriveUseCase<>));
  79. services.AddScoped(typeof(IAddGpuUseCase<>), typeof(AddGpuUseCase<>));
  80. services.AddScoped(typeof(IRemoveGpuUseCase<>), typeof(RemoveGpuUseCase<>));
  81. services.AddScoped(typeof(IUpdateGpuUseCase<>), typeof(UpdateGpuUseCase<>));
  82. services.AddScoped(typeof(IAddPortUseCase<>), typeof(AddPortUseCase<>));
  83. services.AddScoped(typeof(IRemovePortUseCase<>), typeof(RemovePortUseCase<>));
  84. services.AddScoped(typeof(IUpdatePortUseCase<>), typeof(UpdatePortUseCase<>));
  85. services.AddScoped(typeof(IAddConnectionUseCase), typeof(AddConnectionUseCase));
  86. services.AddScoped(typeof(IGetConnectionForPortUseCase), typeof(GetConnectionForPortUseCase));
  87. services.AddScoped(typeof(IGetConnectionsForResourceUseCase), typeof(GetConnectionsForResourceUseCase));
  88. services.AddScoped(typeof(IRemoveConnectionUseCase), typeof(RemoveConnectionUseCase));
  89. IEnumerable<Type>? usecases = Assembly.GetAssembly(typeof(IUseCase))
  90. ?.GetTypes()
  91. .Where(t =>
  92. !t.IsAbstract &&
  93. typeof(IUseCase).IsAssignableFrom(t)
  94. );
  95. if (usecases != null)
  96. foreach (Type type in usecases)
  97. services.AddScoped(type);
  98. return services;
  99. }
  100. public static IServiceCollection AddYamlRepos(
  101. this IServiceCollection services) {
  102. services.AddScoped<IHardwareRepository, YamlHardwareRepository>();
  103. services.AddScoped<ISystemRepository, YamlSystemRepository>();
  104. services.AddScoped<IServiceRepository, ServiceRepository>();
  105. return services;
  106. }
  107. }