using System.Linq; using Autofac; using Autofac.Builder; 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 registration builder of this new task. That can be used to edit the registration. public static IRegistrationBuilder RegisterTask(this ContainerBuilder builder) where T : class, ITask { return builder.RegisterType().As().SingleInstance(); } /// /// Register a new metadata provider to the container. /// /// The container /// The type of the task /// The registration builder of this new provider. That can be used to edit the registration. public static IRegistrationBuilder RegisterProvider(this ContainerBuilder builder) where T : class, IMetadataProvider { return builder.RegisterType().As().InstancePerLifetimeScope(); } /// /// Register a new repository to the container. /// /// The container /// The type of the repository. /// /// If your repository implements a special interface, please use /// /// The initial container. public static IRegistrationBuilder RegisterRepository(this ContainerBuilder builder) where T : IBaseRepository { return builder.RegisterType() .As() .As(Utility.GetGenericDefinition(typeof(T), typeof(IRepository<>))) .InstancePerLifetimeScope(); } /// /// Register a new repository with a custom mapping to the container. /// /// The container /// 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 IRegistrationBuilder RegisterRepository(this ContainerBuilder builder) where T2 : IBaseRepository, T { return builder.RegisterRepository().As(); } /// /// 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"; } } }