Implemented frontend hosting file generation helper

This commit is contained in:
2025-03-16 22:03:01 +01:00
parent 1238095f09
commit 75f037da02
6 changed files with 296 additions and 60 deletions

View File

@@ -0,0 +1,27 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MoonCore.Extended.PermFilter;
using Moonlight.ApiServer.Services;
namespace Moonlight.ApiServer.Http.Controllers.Admin.Sys;
[Authorize]
[ApiController]
[Route("api/admin/system/advanced")]
public class AdvancedController : Controller
{
private readonly FrontendService FrontendService;
public AdvancedController(FrontendService frontendService)
{
FrontendService = frontendService;
}
[HttpGet("frontend")]
[RequirePermission("admin.system.advanced.frontend")]
public async Task Frontend()
{
var stream = await FrontendService.GenerateZip();
await Results.File(stream, fileDownloadName: "frontend.zip").ExecuteAsync(HttpContext);
}
}

View File

@@ -11,71 +11,18 @@ namespace Moonlight.ApiServer.Http.Controllers;
[ApiController]
public class FrontendController : Controller
{
private readonly AppConfiguration Configuration;
private readonly FrontendService FrontendService;
private readonly PluginService PluginService;
public FrontendController(
AppConfiguration configuration,
PluginService pluginService
)
public FrontendController(FrontendService frontendService, PluginService pluginService)
{
Configuration = configuration;
FrontendService = frontendService;
PluginService = pluginService;
}
[HttpGet("frontend.json")]
public async Task<FrontendConfiguration> GetConfiguration()
{
var configuration = new FrontendConfiguration()
{
Title = "Moonlight",
ApiUrl = Configuration.PublicUrl,
HostEnvironment = "ApiServer"
};
#region Load theme.json if it exists
var themePath = PathBuilder.File("storage", "theme.json");
if (System.IO.File.Exists(themePath))
{
var variablesJson = await System.IO.File.ReadAllTextAsync(themePath);
configuration.Theme.Variables =
JsonSerializer.Deserialize<Dictionary<string, string>>(variablesJson) ?? new();
}
#endregion
// Collect assemblies for the 'client' section
configuration.Assemblies = PluginService
.GetAssemblies("client")
.Keys
.ToArray();
// Collect scripts to execute
configuration.Scripts = PluginService
.LoadedPlugins
.Keys
.SelectMany(x => x.Scripts)
.ToArray();
// Collect styles
var styles = new List<string>();
styles.AddRange(
PluginService
.LoadedPlugins
.Keys
.SelectMany(x => x.Styles)
);
// Add bundle css
styles.Add("css/bundle.min.css");
configuration.Styles = styles.ToArray();
return configuration;
}
=> await FrontendService.GetConfiguration();
[HttpGet("plugins/{assemblyName}")]
public async Task GetPluginAssembly(string assemblyName)