From 0f00c22f7f9433083c8877a63278e87688646780 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 17 May 2021 23:41:54 +0200 Subject: [PATCH] Cleaning up the startup --- Kyoo.Common/Module.cs | 1 - Kyoo/CoreModule.cs | 47 ++++++++++++++++++++++++++++++++++ Kyoo/Startup.cs | 40 +++++------------------------ Kyoo/Views/ConfigurationApi.cs | 27 ++++++++++++++++++- 4 files changed, 79 insertions(+), 36 deletions(-) diff --git a/Kyoo.Common/Module.cs b/Kyoo.Common/Module.cs index e8e23035..e8fcf1b6 100644 --- a/Kyoo.Common/Module.cs +++ b/Kyoo.Common/Module.cs @@ -2,7 +2,6 @@ using System; using System.Linq; using Kyoo.Controllers; using Kyoo.Models; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace Kyoo diff --git a/Kyoo/CoreModule.cs b/Kyoo/CoreModule.cs index 3111177e..3c637fce 100644 --- a/Kyoo/CoreModule.cs +++ b/Kyoo/CoreModule.cs @@ -1,10 +1,15 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using Kyoo.Controllers; using Kyoo.Models.Permissions; using Kyoo.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.StaticFiles; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.FileProviders; namespace Kyoo { @@ -66,9 +71,34 @@ namespace Kyoo typeof(IProviderRepository) }; + + /// + /// The configuration to use. + /// + private readonly IConfiguration _configuration; + + + /// + /// Create a new core module instance and use the given configuration. + /// + /// The configuration to use + public CoreModule(IConfiguration configuration) + { + _configuration = configuration; + } + /// public void Configure(IServiceCollection services, ICollection availableTypes) { + string publicUrl = _configuration.GetValue("publicUrl"); + + services.AddControllers() + .AddNewtonsoftJson(x => + { + x.SerializerSettings.ContractResolver = new JsonPropertyIgnorer(publicUrl); + x.SerializerSettings.Converters.Add(new PeopleRoleConverter()); + }); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); @@ -99,5 +129,22 @@ namespace Kyoo if (services.All(x => x.ServiceType != typeof(IPermissionValidator))) services.AddSingleton(); } + + /// + 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(); + }); + } } } \ No newline at end of file diff --git a/Kyoo/Startup.cs b/Kyoo/Startup.cs index e30f495e..361c1a27 100644 --- a/Kyoo/Startup.cs +++ b/Kyoo/Startup.cs @@ -8,10 +8,8 @@ using Kyoo.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.SpaServices.AngularCli; -using Microsoft.AspNetCore.StaticFiles; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -22,10 +20,6 @@ namespace Kyoo /// public class Startup { - /// - /// The configuration context - /// - private readonly IConfiguration _configuration; /// /// A plugin manager used to load plugins and allow them to configure services / asp net. /// @@ -44,11 +38,11 @@ namespace Kyoo /// A logger factory used to create a logger for the plugin manager. public Startup(IServiceProvider hostProvider, IConfiguration configuration, ILoggerFactory loggerFactory, IWebHostEnvironment host) { - _configuration = configuration; - _plugins = new PluginManager(hostProvider, _configuration, loggerFactory.CreateLogger()); + _plugins = new PluginManager(hostProvider, configuration, loggerFactory.CreateLogger()); // 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 AuthenticationModule(configuration, loggerFactory, host) }); @@ -60,8 +54,6 @@ namespace Kyoo /// The service collection to fill. public void ConfigureServices(IServiceCollection services) { - string publicUrl = _configuration.GetValue("publicUrl"); - services.AddMvc().AddControllersAsServices(); services.AddSpaStaticFiles(configuration => @@ -72,13 +64,7 @@ namespace Kyoo { x.EnableForHttps = true; }); - - services.AddControllers() - .AddNewtonsoftJson(x => - { - x.SerializerSettings.ContractResolver = new JsonPropertyIgnorer(publicUrl); - x.SerializerSettings.Converters.Add(new PeopleRoleConverter()); - }); + services.AddHttpClient(); services.AddTransient(typeof(Lazy<>), typeof(LazyDi<>)); @@ -99,22 +85,14 @@ namespace Kyoo app.UseDeveloperExceptionPage(); else { - app.UseExceptionHandler("/Error"); + app.UseExceptionHandler("/error"); 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()) app.UseSpaStaticFiles(); app.UseRouting(); - app.Use((ctx, next) => { ctx.Response.Headers.Remove("X-Powered-By"); @@ -131,12 +109,6 @@ namespace Kyoo _plugins.ConfigureAspnet(app); - app.UseEndpoints(endpoints => - { - endpoints.MapControllers(); - }); - - app.UseSpa(spa => { spa.Options.SourcePath = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "Kyoo.WebApp"); diff --git a/Kyoo/Views/ConfigurationApi.cs b/Kyoo/Views/ConfigurationApi.cs index ef346979..83cd80d5 100644 --- a/Kyoo/Views/ConfigurationApi.cs +++ b/Kyoo/Views/ConfigurationApi.cs @@ -40,8 +40,10 @@ namespace Kyoo.Api /// /// Get a permission from it's slug. /// - /// The permission to retrieve. You can use __ to get a child value. + /// The permission to retrieve. You can use ':' or "__" to get a child value. /// The associate value or list of values. + /// Return the configuration value or the list of configurations + /// No configuration exists for the given slug [HttpGet("{slug}")] [Permission(nameof(ConfigurationApi), Kind.Admin)] public ActionResult GetConfiguration(string slug) @@ -57,5 +59,28 @@ namespace Kyoo.Api _configuration.Bind(slug, option); return option; } + + /// + /// Edit a permission from it's slug. + /// + /// The permission to edit. You can use ':' or "__" to get a child value. + /// The new value of the configuration + /// The edited value. + /// Return the edited value + /// No configuration exists for the given slug + [HttpPut("{slug}")] + [Permission(nameof(ConfigurationApi), Kind.Admin)] + public ActionResult 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; + } } } \ No newline at end of file