using System; using System.Linq; using Kyoo.Controllers; using Kyoo.Models; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace Kyoo { /// /// A static class with helper functions to setup external modules /// public static class Module { /// /// Register a new task to the container. /// /// The container /// The type of the task /// The initial container. public static IServiceCollection AddTask(this IServiceCollection services) where T : class, ITask { services.AddSingleton(); return services; } /// /// Register a new repository to the container. /// /// The container /// The lifetime of the repository. The default is scoped. /// The type of the repository. /// /// If your repository implements a special interface, please use /// /// The initial container. public static IServiceCollection AddRepository(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Scoped) where T : IBaseRepository { Type repository = Utility.GetGenericDefinition(typeof(T), typeof(IRepository<>)); if (repository != null) services.Add(ServiceDescriptor.Describe(repository, typeof(T), lifetime)); services.Add(ServiceDescriptor.Describe(typeof(IBaseRepository), typeof(T), lifetime)); return services; } /// /// Register a new repository with a custom mapping to the container. /// /// /// The lifetime of the repository. The default is scoped. /// The custom mapping you have for your repository. /// The type of the repository. /// /// If your repository does not implements a special interface, please use /// /// The initial container. public static IServiceCollection AddRepository(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Scoped) where T2 : IBaseRepository, T { services.Add(ServiceDescriptor.Describe(typeof(T), typeof(T2), lifetime)); return services.AddRepository(lifetime); } /// /// Add an editable configuration to the editable configuration list /// /// The service collection to edit /// The root path of the editable configuration. It should not be a nested type. /// The type of the configuration /// The given service collection is returned. public static IServiceCollection AddConfiguration(this IServiceCollection services, string path) where T : class { if (services.Any(x => x.ServiceType == typeof(T))) return services; foreach (ConfigurationReference confRef in ConfigurationReference.CreateReference(path)) services.AddSingleton(confRef); return services; } /// /// Add an editable configuration to the editable configuration list. /// WARNING: this method allow you to add an unmanaged type. This type won't be editable. This can be used /// for external libraries or variable arguments. /// /// The service collection to edit /// The root path of the editable configuration. It should not be a nested type. /// The given service collection is returned. public static IServiceCollection AddUntypedConfiguration(this IServiceCollection services, string path) { services.AddSingleton(ConfigurationReference.CreateUntyped(path)); return services; } /// /// Get the public URL of kyoo using the given configuration instance. /// /// The configuration instance /// The public URl of kyoo (without a slash at the end) public static string GetPublicUrl(this IConfiguration configuration) { return configuration["basics:publicUrl"]?.TrimEnd('/') ?? "http://localhost:5000"; } } }