ServiceCollectionExtensions.cs 6.1 KB

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