Cleaning up the startup

This commit is contained in:
Zoe Roux 2021-05-17 23:41:54 +02:00
parent 1929994128
commit 0f00c22f7f
4 changed files with 79 additions and 36 deletions

View File

@ -2,7 +2,6 @@ using System;
using System.Linq; using System.Linq;
using Kyoo.Controllers; using Kyoo.Controllers;
using Kyoo.Models; using Kyoo.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
namespace Kyoo namespace Kyoo

View File

@ -1,10 +1,15 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using Kyoo.Controllers; using Kyoo.Controllers;
using Kyoo.Models.Permissions; using Kyoo.Models.Permissions;
using Kyoo.Tasks; using Kyoo.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
namespace Kyoo namespace Kyoo
{ {
@ -66,9 +71,34 @@ namespace Kyoo
typeof(IProviderRepository) typeof(IProviderRepository)
}; };
/// <summary>
/// The configuration to use.
/// </summary>
private readonly IConfiguration _configuration;
/// <summary>
/// Create a new core module instance and use the given configuration.
/// </summary>
/// <param name="configuration">The configuration to use</param>
public CoreModule(IConfiguration configuration)
{
_configuration = configuration;
}
/// <inheritdoc /> /// <inheritdoc />
public void Configure(IServiceCollection services, ICollection<Type> availableTypes) public void Configure(IServiceCollection services, ICollection<Type> availableTypes)
{ {
string publicUrl = _configuration.GetValue<string>("publicUrl");
services.AddControllers()
.AddNewtonsoftJson(x =>
{
x.SerializerSettings.ContractResolver = new JsonPropertyIgnorer(publicUrl);
x.SerializerSettings.Converters.Add(new PeopleRoleConverter());
});
services.AddSingleton<IFileManager, FileManager>(); services.AddSingleton<IFileManager, FileManager>();
services.AddSingleton<ITranscoder, Transcoder>(); services.AddSingleton<ITranscoder, Transcoder>();
services.AddSingleton<IThumbnailsManager, ThumbnailsManager>(); services.AddSingleton<IThumbnailsManager, ThumbnailsManager>();
@ -99,5 +129,22 @@ namespace Kyoo
if (services.All(x => x.ServiceType != typeof(IPermissionValidator))) if (services.All(x => x.ServiceType != typeof(IPermissionValidator)))
services.AddSingleton<IPermissionValidator, PassthroughPermissionValidator>(); services.AddSingleton<IPermissionValidator, PassthroughPermissionValidator>();
} }
/// <inheritdoc />
public void ConfigureAspNet(IApplicationBuilder app)
{
FileExtensionContentTypeProvider contentTypeProvider = new();
contentTypeProvider.Mappings[".data"] = "application/octet-stream";
app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = contentTypeProvider,
FileProvider = new PhysicalFileProvider(Path.Join(AppDomain.CurrentDomain.BaseDirectory, "wwwroot"))
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
} }
} }

View File

@ -8,10 +8,8 @@ using Kyoo.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SpaServices.AngularCli; using Microsoft.AspNetCore.SpaServices.AngularCli;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -22,10 +20,6 @@ namespace Kyoo
/// </summary> /// </summary>
public class Startup public class Startup
{ {
/// <summary>
/// The configuration context
/// </summary>
private readonly IConfiguration _configuration;
/// <summary> /// <summary>
/// A plugin manager used to load plugins and allow them to configure services / asp net. /// A plugin manager used to load plugins and allow them to configure services / asp net.
/// </summary> /// </summary>
@ -44,11 +38,11 @@ namespace Kyoo
/// <param name="loggerFactory">A logger factory used to create a logger for the plugin manager.</param> /// <param name="loggerFactory">A logger factory used to create a logger for the plugin manager.</param>
public Startup(IServiceProvider hostProvider, IConfiguration configuration, ILoggerFactory loggerFactory, IWebHostEnvironment host) public Startup(IServiceProvider hostProvider, IConfiguration configuration, ILoggerFactory loggerFactory, IWebHostEnvironment host)
{ {
_configuration = configuration; _plugins = new PluginManager(hostProvider, configuration, loggerFactory.CreateLogger<PluginManager>());
_plugins = new PluginManager(hostProvider, _configuration, loggerFactory.CreateLogger<PluginManager>());
// TODO remove postgres from here and load it like a normal plugin. // TODO remove postgres from here and load it like a normal plugin.
_plugins.LoadPlugins(new IPlugin[] {new CoreModule(), _plugins.LoadPlugins(new IPlugin[] {
new CoreModule(configuration),
new PostgresModule(configuration, host), new PostgresModule(configuration, host),
new AuthenticationModule(configuration, loggerFactory, host) new AuthenticationModule(configuration, loggerFactory, host)
}); });
@ -60,8 +54,6 @@ namespace Kyoo
/// <param name="services">The service collection to fill.</param> /// <param name="services">The service collection to fill.</param>
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
string publicUrl = _configuration.GetValue<string>("publicUrl");
services.AddMvc().AddControllersAsServices(); services.AddMvc().AddControllersAsServices();
services.AddSpaStaticFiles(configuration => services.AddSpaStaticFiles(configuration =>
@ -72,13 +64,7 @@ namespace Kyoo
{ {
x.EnableForHttps = true; x.EnableForHttps = true;
}); });
services.AddControllers()
.AddNewtonsoftJson(x =>
{
x.SerializerSettings.ContractResolver = new JsonPropertyIgnorer(publicUrl);
x.SerializerSettings.Converters.Add(new PeopleRoleConverter());
});
services.AddHttpClient(); services.AddHttpClient();
services.AddTransient(typeof(Lazy<>), typeof(LazyDi<>)); services.AddTransient(typeof(Lazy<>), typeof(LazyDi<>));
@ -99,22 +85,14 @@ namespace Kyoo
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
else else
{ {
app.UseExceptionHandler("/Error"); app.UseExceptionHandler("/error");
app.UseHsts(); app.UseHsts();
} }
FileExtensionContentTypeProvider contentTypeProvider = new();
contentTypeProvider.Mappings[".data"] = "application/octet-stream";
app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = contentTypeProvider,
FileProvider = new PhysicalFileProvider(Path.Join(AppDomain.CurrentDomain.BaseDirectory, "wwwroot"))
});
if (!env.IsDevelopment()) if (!env.IsDevelopment())
app.UseSpaStaticFiles(); app.UseSpaStaticFiles();
app.UseRouting(); app.UseRouting();
app.Use((ctx, next) => app.Use((ctx, next) =>
{ {
ctx.Response.Headers.Remove("X-Powered-By"); ctx.Response.Headers.Remove("X-Powered-By");
@ -131,12 +109,6 @@ namespace Kyoo
_plugins.ConfigureAspnet(app); _plugins.ConfigureAspnet(app);
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseSpa(spa => app.UseSpa(spa =>
{ {
spa.Options.SourcePath = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "Kyoo.WebApp"); spa.Options.SourcePath = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "Kyoo.WebApp");

View File

@ -40,8 +40,10 @@ namespace Kyoo.Api
/// <summary> /// <summary>
/// Get a permission from it's slug. /// Get a permission from it's slug.
/// </summary> /// </summary>
/// <param name="slug">The permission to retrieve. You can use __ to get a child value.</param> /// <param name="slug">The permission to retrieve. You can use ':' or "__" to get a child value.</param>
/// <returns>The associate value or list of values.</returns> /// <returns>The associate value or list of values.</returns>
/// <response code="200">Return the configuration value or the list of configurations</response>
/// <response code="404">No configuration exists for the given slug</response>
[HttpGet("{slug}")] [HttpGet("{slug}")]
[Permission(nameof(ConfigurationApi), Kind.Admin)] [Permission(nameof(ConfigurationApi), Kind.Admin)]
public ActionResult<object> GetConfiguration(string slug) public ActionResult<object> GetConfiguration(string slug)
@ -57,5 +59,28 @@ namespace Kyoo.Api
_configuration.Bind(slug, option); _configuration.Bind(slug, option);
return option; return option;
} }
/// <summary>
/// Edit a permission from it's slug.
/// </summary>
/// <param name="slug">The permission to edit. You can use ':' or "__" to get a child value.</param>
/// <param name="newValue">The new value of the configuration</param>
/// <returns>The edited value.</returns>
/// <response code="200">Return the edited value</response>
/// <response code="404">No configuration exists for the given slug</response>
[HttpPut("{slug}")]
[Permission(nameof(ConfigurationApi), Kind.Admin)]
public ActionResult<object> EditConfiguration(string slug, [FromBody] object newValue)
{
slug = slug.Replace("__", ":");
if (!_references.TryGetValue(slug, out Type type))
return NotFound();
// object ret = _configuration.(type, slug);
// if (ret != null)
// return ret;
// object option = Activator.CreateInstance(type);
// _configuration.Bind(slug, option);
return newValue;
}
} }
} }