// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see .
using System;
using System.Collections.Generic;
using System.Linq;
using Autofac;
using Kyoo.Abstractions;
using Kyoo.Abstractions.Controllers;
using Kyoo.Authentication;
using Kyoo.Core.Controllers;
using Kyoo.Core.Models.Options;
using Kyoo.Core.Tasks;
using Kyoo.Postgresql;
using Kyoo.SqLite;
using Kyoo.TheMovieDb;
using Kyoo.TheTvdb;
using Kyoo.Utils;
using Kyoo.WebApp;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Kyoo.Core
{
///
/// The Startup class is used to configure the AspNet's webhost.
///
public class PluginsStartup
{
///
/// A plugin manager used to load plugins and allow them to configure services / asp net.
///
private readonly IPluginManager _plugins;
///
/// The configuration used to register and so on for plugin's specified types.
///
private readonly IConfiguration _configuration;
///
/// Created from the DI container, those services are needed to load information and instantiate plugins.s
///
/// The plugin manager to use to load new plugins and configure the host.
///
/// The configuration used to register and so on for plugin's specified types.
///
public PluginsStartup(IPluginManager plugins, IConfiguration configuration)
{
_plugins = plugins;
_configuration = configuration;
_plugins.LoadPlugins(
typeof(CoreModule),
typeof(WebAppModule),
typeof(AuthenticationModule),
typeof(PostgresModule),
typeof(SqLiteModule),
typeof(PluginTvdb),
typeof(PluginTmdb)
);
}
///
/// Create a new from a webhost.
/// This is meant to be used from .
///
/// The context of the web host.
///
/// The logger factory used to log while the application is setting itself up.
///
/// A new .
public static PluginsStartup FromWebHost(WebHostBuilderContext host,
ILoggerFactory logger)
{
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, host.Configuration);
}
///
/// Configure the services context via the .
///
/// The service collection to fill.
public void ConfigureServices(IServiceCollection services)
{
foreach (IPlugin plugin in _plugins.GetAllPlugins())
plugin.Configure(services);
IEnumerable> configTypes = _plugins.GetAllPlugins()
.SelectMany(x => x.Configuration)
.Where(x => x.Value != null);
foreach ((string path, Type type) in configTypes)
{
Utility.RunGenericMethod