diff --git a/Moonlight.ApiServer/Http/Controllers/FrontendController.cs b/Moonlight.ApiServer/Http/Controllers/FrontendController.cs index 75ba881d..ddd519b3 100644 --- a/Moonlight.ApiServer/Http/Controllers/FrontendController.cs +++ b/Moonlight.ApiServer/Http/Controllers/FrontendController.cs @@ -29,11 +29,14 @@ public class FrontendController : Controller { var assembliesMap = PluginService.GetAssemblies("client"); - if (assembliesMap.ContainsKey(assemblyName)) + if (!assembliesMap.TryGetValue(assemblyName, out var path)) throw new HttpApiException("The requested assembly could not be found", 404); - var path = assembliesMap[assemblyName]; + var absolutePath = Path.Combine( + Directory.GetCurrentDirectory(), + path + ); - await Results.File(path).ExecuteAsync(HttpContext); + await Results.File(absolutePath).ExecuteAsync(HttpContext); } } \ No newline at end of file diff --git a/Moonlight.ApiServer/Http/Middleware/ApiErrorMiddleware.cs b/Moonlight.ApiServer/Http/Middleware/ApiErrorMiddleware.cs deleted file mode 100644 index 7a0cecd1..00000000 --- a/Moonlight.ApiServer/Http/Middleware/ApiErrorMiddleware.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System.Diagnostics; -using System.Net.Sockets; -using MoonCore.Exceptions; - -namespace Moonlight.ApiServer.Http.Middleware; - -public class ApiErrorMiddleware -{ - private readonly RequestDelegate Next; - - public ApiErrorMiddleware(RequestDelegate next) - { - Next = next; - } - - public async Task Invoke(HttpContext context) - { - try - { - await Next(context); - } - catch (HttpApiException httpApiException) - { - await Results.Problem( - title: httpApiException.Title, - detail: httpApiException.Detail, - statusCode: httpApiException.Status, - type: "moonlight/general-api-error" - ).ExecuteAsync(context); - } - catch (HttpRequestException e) - { - var logger = context.RequestServices.GetRequiredService>(); - - if (e.InnerException is SocketException) - { - logger.LogCritical("An unhandled socket exception occured. [{method}] {path}: {e}", context.Request.Method, context.Request.Path, e); - - await Results.Problem( - title: "An socket exception occured on the api server", - detail: "Check the api server logs for more details", - statusCode: 502, - type: "moonlight/remote-api-connection-error" - ).ExecuteAsync(context); - - return; - } - - logger.LogCritical("An unhandled exception occured. [{method}] {path}: {e}", context.Request.Method, context.Request.Path, e.Demystify()); - - await Results.Problem( - title: "An http request exception occured on the api server", - detail: "Check the api server logs for more details", - statusCode: 500, - type: "moonlight/remote-api-request-error" - ).ExecuteAsync(context); - } - catch (Exception e) - { - var logger = context.RequestServices.GetRequiredService>(); - - logger.LogCritical("An unhandled exception occured. [{method}] {path}: {e}", context.Request.Method, context.Request.Path, e); - - await Results.Problem( - title: "An unhanded exception occured on the api server", - detail: "Check the api server logs for more details", - statusCode: 500, - type: "moonlight/critical-api-error" - ).ExecuteAsync(context); - } - } -} \ No newline at end of file diff --git a/Moonlight.ApiServer/Services/BundleGenerationService.cs b/Moonlight.ApiServer/Services/BundleGenerationService.cs index b736b6d5..cbfc27c0 100644 --- a/Moonlight.ApiServer/Services/BundleGenerationService.cs +++ b/Moonlight.ApiServer/Services/BundleGenerationService.cs @@ -8,17 +8,20 @@ public class BundleGenerationService : IHostedService { private readonly ILogger Logger; private readonly IWebHostEnvironment HostEnvironment; + private readonly PluginService PluginService; private readonly BundleService BundleService; public BundleGenerationService( ILogger logger, IWebHostEnvironment hostEnvironment, - BundleService bundleService + BundleService bundleService, + PluginService pluginService ) { Logger = logger; HostEnvironment = hostEnvironment; BundleService = bundleService; + PluginService = pluginService; } private async Task Bundle(CancellationToken cancellationToken) @@ -34,29 +37,36 @@ public class BundleGenerationService : IHostedService if (fileInfo is NotFoundFileInfo || fileInfo.PhysicalPath == null) { - Logger.LogWarning( - "Unable to find physical path for the requested css file '{file}'. Make sure its inside a wwwroot folder", - cssFile - ); - - continue; + fileInfo = PluginService.WwwRootFileProvider.GetFileInfo(cssFile); + + if (fileInfo is NotFoundFileInfo || fileInfo.PhysicalPath == null) + { + Logger.LogWarning( + "Unable to find physical path for the requested css file '{file}'. Make sure its inside a wwwroot folder", + cssFile + ); + + continue; + } } - + Logger.LogTrace("Discovered css file '{path}' at '{physicalPath}'", cssFile, fileInfo.PhysicalPath); physicalCssFiles.Add(fileInfo.PhysicalPath); } - - if(physicalCssFiles.Count == 0) - Logger.LogWarning("No physical paths to css files loaded. The generated bundle will be empty. Unless this is intended by you this is a bug"); + + if (physicalCssFiles.Count == 0) + Logger.LogWarning( + "No physical paths to css files loaded. The generated bundle will be empty. Unless this is intended by you this is a bug"); // TODO: Implement cache - + // TODO: File system watcher for development var bundleContent = await CreateCssBundle(physicalCssFiles); Directory.CreateDirectory(PathBuilder.Dir("storage", "tmp")); + await File.WriteAllTextAsync(PathBuilder.File("storage", "tmp", "bundle.css"), bundleContent, cancellationToken); @@ -83,7 +93,7 @@ public class BundleGenerationService : IHostedService { var fileContent = await File.ReadAllTextAsync(physicalPath); var stylesheet = await parser.ParseAsync(fileContent); - + // Check if it's the first stylesheet we are loading if (mainStylesheet == null || content == null) { @@ -106,7 +116,7 @@ public class BundleGenerationService : IHostedService Logger.LogError("An unable to delegate main stylesheet. Did every load attempt of an stylesheet fail?"); return ""; } - + // Process stylesheets against the main one foreach (var stylesheet in additionalStyleSheets) { diff --git a/Moonlight.ApiServer/Startup.cs b/Moonlight.ApiServer/Startup.cs index 068f66f7..d97f713e 100644 --- a/Moonlight.ApiServer/Startup.cs +++ b/Moonlight.ApiServer/Startup.cs @@ -236,7 +236,9 @@ public class Startup { try { - PluginLoadContext.LoadFromAssemblyPath(assemblyFile); + PluginLoadContext.LoadFromAssemblyPath( + Path.Combine(Directory.GetCurrentDirectory(), assemblyFile) + ); } catch (Exception e) { @@ -647,7 +649,7 @@ public class Startup "Hangfire.Server.BackgroundServerProcess", LogLevel.Warning ); - + WebApplicationBuilder.Logging.AddFilter( "Hangfire.BackgroundJobServer", LogLevel.Warning @@ -660,7 +662,7 @@ public class Startup { if (WebApplication.Environment.IsDevelopment()) WebApplication.UseHangfireDashboard(); - + return Task.CompletedTask; }