diff --git a/Kyoo.Common/Controllers/IPluginManager.cs b/Kyoo.Common/Controllers/IPluginManager.cs index 04d308f3..de439cc2 100644 --- a/Kyoo.Common/Controllers/IPluginManager.cs +++ b/Kyoo.Common/Controllers/IPluginManager.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using Autofac; using Kyoo.Models.Exceptions; @@ -41,6 +42,15 @@ namespace Kyoo.Controllers /// You should not try to put plugins from the plugins directory here as they will get automatically loaded. /// public void LoadPlugins(ICollection plugins); + + /// + /// Load plugins and their dependencies from the plugin directory. + /// + /// + /// An initial plugin list to use. + /// You should not try to put plugins from the plugins directory here as they will get automatically loaded. + /// + public void LoadPlugins(params Type[] plugins); /// /// Configure container adding or removing services as the plugins wants. diff --git a/Kyoo/Controllers/PluginManager.cs b/Kyoo/Controllers/PluginManager.cs index 646c644d..215b0c9b 100644 --- a/Kyoo/Controllers/PluginManager.cs +++ b/Kyoo/Controllers/PluginManager.cs @@ -135,6 +135,12 @@ namespace Kyoo.Controllers _logger.LogInformation("Plugin enabled: {Plugins}", _plugins.Select(x => x.Name)); } + /// + public void LoadPlugins(params Type[] plugins) + { + throw new NotImplementedException(); + } + /// public void ConfigureContainer(ContainerBuilder builder) { diff --git a/Kyoo/Startup.cs b/Kyoo/PluginsStartup.cs similarity index 76% rename from Kyoo/Startup.cs rename to Kyoo/PluginsStartup.cs index cd9d4175..e279cb57 100644 --- a/Kyoo/Startup.cs +++ b/Kyoo/PluginsStartup.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using Autofac; using Autofac.Extras.AttributeMetadata; @@ -6,6 +7,7 @@ using Kyoo.Authentication; using Kyoo.Controllers; using Kyoo.Models.Options; using Kyoo.Postgresql; +using Kyoo.SqLite; using Kyoo.Tasks; using Kyoo.TheMovieDb; using Kyoo.TheTvdb; @@ -23,43 +25,28 @@ namespace Kyoo /// /// The Startup class is used to configure the AspNet's webhost. /// - public class Startup + public class PluginsStartup { /// /// A plugin manager used to load plugins and allow them to configure services / asp net. /// private readonly IPluginManager _plugins; - - + /// /// Created from the DI container, those services are needed to load information and instantiate plugins.s /// - /// - /// The host environment that could be used by plugins to configure themself. - /// - /// The configuration context - /// A logger factory used to create a logger for the plugin manager. - public Startup(IWebHostEnvironment hostEnvironment, - IConfiguration configuration, - ILoggerFactory loggerFactory) + /// The plugin manager to use to load new plugins and configure the host. + public PluginsStartup(IPluginManager plugins) { - HostServiceProvider hostProvider = new(hostEnvironment, configuration, loggerFactory); - _plugins = new PluginManager( - hostProvider, - Options.Create(configuration.GetSection(BasicOptions.Path).Get()), - loggerFactory.CreateLogger() + _plugins = plugins; + _plugins.LoadPlugins( + typeof(CoreModule), + typeof(AuthenticationModule), + typeof(PostgresModule), + typeof(SqLiteModule), + typeof(PluginTvdb), + typeof(PluginTmdb) ); - - // TODO maybe keep all core-plugins here to simplify the build process but use their typeof in the method - // (to allow simple constructor changes), leaving the instantiation responsibility to the plugin manager. - _plugins.LoadPlugins(new IPlugin[] { - new CoreModule(configuration), - new PostgresModule(configuration, hostEnvironment), - // new SqLiteModule(configuration, host), - new AuthenticationModule(configuration, loggerFactory, hostEnvironment), - new PluginTvdb(configuration), - new PluginTmdb(configuration) - }); } /// @@ -142,6 +129,34 @@ namespace Kyoo }); } + + /// + /// Create a new from a webhost. + /// This is meant to be used from . + /// + /// The context of the web host. + /// + /// The method used to configure the logger factory used by the plugin manager and plugins during startup. + /// + /// A new . + public static PluginsStartup FromWebHost(WebHostBuilderContext host, + Action loggingConfiguration) + { + HostBuilderContext genericHost = new(new Dictionary()) + { + Configuration = host.Configuration, + HostingEnvironment = host.HostingEnvironment + }; + ILoggerFactory logger = LoggerFactory.Create(builder => loggingConfiguration(genericHost, builder)); + HostServiceProvider hostProvider = new(host.HostingEnvironment, host.Configuration, logger); + PluginManager plugins = new( + hostProvider, + Options.Create(host.Configuration.GetSection(BasicOptions.Path).Get()), + logger.CreateLogger() + ); + return new PluginsStartup(plugins); + } + /// /// A simple host service provider used to activate plugins instance. /// The same services as a generic host are available and an has been added. diff --git a/Kyoo/Program.cs b/Kyoo/Program.cs index 509ae5f6..4e618efc 100644 --- a/Kyoo/Program.cs +++ b/Kyoo/Program.cs @@ -4,6 +4,7 @@ using System.Diagnostics.CodeAnalysis; using System.IO; using System.Threading.Tasks; using Autofac.Extensions.DependencyInjection; +using Kyoo.Controllers; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -124,26 +125,8 @@ namespace Kyoo .UseIIS() .UseIISIntegration() .UseUrls(configuration.GetValue("basics:url")) - .UseStartup(host => new Startup( - host.HostingEnvironment, - host.Configuration, - LoggerFactory.Create(builder => loggingConfiguration(host.ToGenericHost(), builder))) - ) + .UseStartup(host => PluginsStartup.FromWebHost(host, loggingConfiguration)) ); } - - /// - /// Convert an to a . - /// - /// The to convert. - /// A containing the same properties. - private static HostBuilderContext ToGenericHost(this WebHostBuilderContext host) - { - return new HostBuilderContext(new Dictionary()) - { - Configuration = host.Configuration, - HostingEnvironment = host.HostingEnvironment - }; - } } }