using System; using System.Collections.Generic; using System.IO; using Kyoo.Controllers; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.SpaServices.AngularCli; using Microsoft.AspNetCore.StaticFiles; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace Kyoo.WebApp { /// /// A module to enable the web-app (the front-end of kyoo). /// public class WebAppModule : IPlugin { /// public string Slug => "webapp"; /// public string Name => "WebApp"; /// public string Description => "A module to enable the web-app (the front-end of kyoo)."; /// public Dictionary Configuration => new(); /// public bool Enabled => Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot")); /// /// A logger only used to inform the user if the webapp could not be enabled. /// private readonly ILogger _logger; /// /// Create a new . /// /// A logger only used to inform the user if the webapp could not be enabled. public WebAppModule(ILogger logger) { _logger = logger; } /// public void Disabled() { _logger.LogError("The web app files could not be found, it will be disabled. " + "If you cloned the project, you probably forgot to use the --recurse flag"); } /// public void Configure(IServiceCollection services) { services.AddSpaStaticFiles(x => { x.RootPath = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "wwwroot"); }); } /// public IEnumerable ConfigureSteps => new IStartupAction[] { SA.New((app, env) => { if (!env.IsDevelopment()) app.UseSpaStaticFiles(); }, SA.StaticFiles), SA.New((app, contentTypeProvider) => { app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = contentTypeProvider, FileProvider = new PhysicalFileProvider(Path.Join(AppDomain.CurrentDomain.BaseDirectory, "wwwroot")) }); }, SA.StaticFiles), SA.New(app => { app.Use((ctx, next) => { ctx.Response.Headers.Remove("X-Powered-By"); ctx.Response.Headers.Remove("Server"); ctx.Response.Headers.Add("Feature-Policy", "autoplay 'self'; fullscreen"); ctx.Response.Headers.Add("Content-Security-Policy", "default-src 'self' blob:; script-src 'self' blob: 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; frame-src 'self'"); ctx.Response.Headers.Add("X-Frame-Options", "SAMEORIGIN"); ctx.Response.Headers.Add("Referrer-Policy", "no-referrer"); ctx.Response.Headers.Add("Access-Control-Allow-Origin", "null"); ctx.Response.Headers.Add("X-Content-Type-Options", "nosniff"); return next(); }); }, SA.Endpoint - 499), SA.New((app, env) => { app.UseSpa(spa => { spa.Options.SourcePath = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "Kyoo.WebApp", "Front"); if (env.IsDevelopment()) spa.UseAngularCliServer("start"); }); }, SA.Endpoint - 500) }; } }