Implementing plugin loading for api server and client

This commit is contained in:
Masu-Baumgartner
2024-11-19 16:28:25 +01:00
parent 072adb5bb1
commit 2d0a0e53c0
10 changed files with 235 additions and 73 deletions

View File

@@ -0,0 +1,63 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using MoonCore.Exceptions;
using MoonCore.Models;
using Moonlight.ApiServer.Services;
namespace Moonlight.ApiServer.Http.Controllers;
[ApiController]
[Route("api/pluginsStream")]
public class PluginsStreamController : Controller
{
private readonly PluginService PluginService;
private readonly IMemoryCache Cache;
public PluginsStreamController(PluginService pluginService, IMemoryCache cache)
{
PluginService = pluginService;
Cache = cache;
}
[HttpGet]
public Task<HostedPluginsManifest> GetManifest()
{
var assembliesMap = Cache.GetOrCreate("clientPluginAssemblies", entry =>
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(15);
return PluginService.GetAssemblies("client");
})!;
var entrypoints = Cache.GetOrCreate("clientPluginEntrypoints", entry =>
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(15);
return PluginService.GetEntrypoints("client");
})!;
return Task.FromResult(new HostedPluginsManifest()
{
Assemblies = assembliesMap.Keys.ToArray(),
Entrypoints = entrypoints
});
}
[HttpGet("stream")]
public async Task GetAssembly([FromQuery(Name = "assembly")] string assembly)
{
var assembliesMap = Cache.GetOrCreate("clientPluginAssemblies", entry =>
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(15);
return PluginService.GetAssemblies("client");
})!;
if (assembliesMap.ContainsKey(assembly))
throw new HttpApiException("The requested assembly could not be found", 404);
var path = assembliesMap[assembly];
await Results.File(path).ExecuteAsync(HttpContext);
}
}

View File

@@ -0,0 +1,11 @@
namespace Moonlight.ApiServer.Models;
public class PluginManifest
{
public string Id { get; set; }
public string Name { get; set; }
public string Author { get; set; }
public string[] Dependencies { get; set; } = [];
public Dictionary<string, string[]> Entrypoints { get; set; } = new();
}

View File

@@ -1,12 +1,7 @@
namespace Moonlight.ApiServer.Models; namespace Moonlight.ApiServer.Models;
public class PluginMeta public class PluginMeta
{ {
public string Id { get; set; } public PluginManifest Manifest { get; set; }
public string Name { get; set; } public string Path { get; set; }
public string Author { get; set; }
public string? DonationUrl { get; set; }
public string? UpdateUrl { get; set; }
public Dictionary<string, string[]> Binaries { get; set; } = new();
} }

View File

@@ -23,7 +23,7 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="MoonCore" Version="1.7.4" /> <PackageReference Include="MoonCore" Version="1.7.8" />
<PackageReference Include="MoonCore.Extended" Version="1.1.7" /> <PackageReference Include="MoonCore.Extended" Version="1.1.7" />
<PackageReference Include="MoonCore.PluginFramework" Version="1.0.4" /> <PackageReference Include="MoonCore.PluginFramework" Version="1.0.4" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" /> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
@@ -38,7 +38,9 @@
<ItemGroup> <ItemGroup>
<Folder Include="Database\Migrations\" /> <Folder Include="Database\Migrations\" />
<Folder Include="storage\plugins\" /> <Folder Include="storage\plugins\MyCoolPlugin\bin\apiserver\" />
<Folder Include="storage\plugins\MyCoolPlugin\bin\client\" />
<Folder Include="storage\plugins\MyCoolPlugin\bin\daemon\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -1,4 +1,4 @@
using System.Text.Json; using System.Text.Json;
using MoonCore.Helpers; using MoonCore.Helpers;
using Moonlight.ApiServer.Models; using Moonlight.ApiServer.Models;
@@ -6,10 +6,10 @@ namespace Moonlight.ApiServer.Services;
public class PluginService public class PluginService
{ {
private readonly Dictionary<string, PluginMeta> Plugins = new(); public readonly List<PluginMeta> Plugins = new();
private readonly ILogger<PluginService> Logger;
private static string PluginsPath = PathBuilder.Dir("storage", "plugins"); private static string PluginsFolder = PathBuilder.Dir("storage", "plugins");
private readonly ILogger<PluginService> Logger;
public PluginService(ILogger<PluginService> logger) public PluginService(ILogger<PluginService> logger)
{ {
@@ -18,58 +18,91 @@ public class PluginService
public async Task Load() public async Task Load()
{ {
Logger.LogInformation("Loading plugins..."); // Load all manifest files
foreach (var pluginFolder in Directory.EnumerateDirectories(PluginsFolder))
foreach (var pluginPath in Directory.EnumerateDirectories(PluginsPath))
{ {
var metaPath = PathBuilder.File(PluginsPath, "meta.json"); var manifestPath = PathBuilder.File(pluginFolder, "plugin.json");
if (!File.Exists(metaPath)) if (!File.Exists(manifestPath))
{ {
Logger.LogWarning("Ignoring folder '{name}'. No meta.json found", pluginPath); Logger.LogWarning("Ignoring '{folder}' because no manifest has been found", pluginFolder);
continue; continue;
} }
var metaContent = await File.ReadAllTextAsync(metaPath); PluginManifest manifest;
PluginMeta meta;
try try
{ {
meta = JsonSerializer.Deserialize<PluginMeta>(metaContent) var manifestText = await File.ReadAllTextAsync(manifestPath);
?? throw new JsonException(); manifest = JsonSerializer.Deserialize<PluginManifest>(manifestText)!;
} }
catch (Exception e) catch (Exception e)
{ {
Logger.LogCritical("Unable to deserialize meta.json: {e}", e); Logger.LogError("An unhandled error occured while loading plugin manifest in '{folder}': {e}",
continue; pluginFolder, e);
break;
} }
Plugins.Add(pluginPath, meta); Logger.LogTrace("Loaded plugin manifest. Id: {id}", manifest.Id);
Plugins.Add(new()
{
Manifest = manifest,
Path = pluginFolder
});
} }
// Check for missing dependencies
var pluginsToNotLoad = new List<string>();
foreach (var plugin in Plugins)
{
foreach (var dependency in plugin.Manifest.Dependencies)
{
// Check if dependency is found
if (Plugins.Any(x => x.Manifest.Id == dependency))
continue;
Logger.LogError(
"Unable to load plugin '{id}' ({path}) because the dependency {dependency} is missing",
plugin.Manifest.Id,
plugin.Path,
dependency
);
pluginsToNotLoad.Add(plugin.Manifest.Id);
break;
}
}
// Remove unloadable plugins from cache
Plugins.RemoveAll(x => pluginsToNotLoad.Contains(x.Manifest.Id));
} }
public Task<Dictionary<string, string[]>> GetBinariesBySection(string section) public Dictionary<string, string> GetAssemblies(string section)
{ {
var bins = Plugins var pathMappings = new Dictionary<string, string>();
.Values
.Where(x => x.Binaries.ContainsKey(section))
.ToDictionary(x => x.Id, x => x.Binaries[section]);
return Task.FromResult(bins); foreach (var plugin in Plugins)
{
foreach (var file in Directory.EnumerateFiles(PathBuilder.Dir(plugin.Path, "bin", section)))
{
if(!file.EndsWith(".dll"))
continue;
var fileName = Path.GetFileName(file);
pathMappings[fileName] = file;
}
}
return pathMappings;
} }
public Task<Stream?> GetBinaryStream(string plugin, string section, string fileName) public string[] GetEntrypoints(string section)
{ {
if (Plugins.All(x => x.Value.Id != plugin)) return Plugins
return Task.FromResult<Stream?>(null); .Where(x => x.Manifest.Entrypoints.ContainsKey(section))
.SelectMany(x => x.Manifest.Entrypoints[section])
var pluginData = Plugins.First(x => x.Value.Id == plugin); .ToArray();
var binaryPath = PathBuilder.File(pluginData.Key, section, fileName);
if (!File.Exists(binaryPath))
return Task.FromResult<Stream?>(null);
var fs = File.OpenRead(binaryPath);
return Task.FromResult<Stream?>(fs);
} }
} }

View File

@@ -14,6 +14,7 @@ using MoonCore.Extended.OAuth2.LocalProvider.Implementations;
using MoonCore.Extensions; using MoonCore.Extensions;
using MoonCore.Helpers; using MoonCore.Helpers;
using MoonCore.PluginFramework.Extensions; using MoonCore.PluginFramework.Extensions;
using MoonCore.Plugins;
using Moonlight.ApiServer.Configuration; using Moonlight.ApiServer.Configuration;
using Moonlight.ApiServer.Database.Entities; using Moonlight.ApiServer.Database.Entities;
using Moonlight.ApiServer.Helpers; using Moonlight.ApiServer.Helpers;
@@ -51,7 +52,7 @@ public static class Startup
Console.WriteLine(); Console.WriteLine();
// Storage i guess // Storage i guess
Directory.CreateDirectory(PathBuilder.Dir("storage")); Directory.CreateDirectory(PathBuilder.Dir("storage"));
// Configure startup logger // Configure startup logger
@@ -69,7 +70,14 @@ public static class Startup
var startupLogger = startupLoggerFactory.CreateLogger("Startup"); var startupLogger = startupLoggerFactory.CreateLogger("Startup");
//TODO: Load plugin // Load plugins
var pluginService = new PluginService(
startupLoggerFactory.CreateLogger<PluginService>()
);
await pluginService.Load();
var pluginAssemblies = await LoadPlugins(pluginService, startupLoggerFactory);
// Configure startup interfaces // Configure startup interfaces
var startupServiceCollection = new ServiceCollection(); var startupServiceCollection = new ServiceCollection();
@@ -94,10 +102,10 @@ public static class Startup
// Configure assemblies to scan // Configure assemblies to scan
configuration.AddAssembly(typeof(Startup).Assembly); configuration.AddAssembly(typeof(Startup).Assembly);
if(additionalAssemblies != null) if (additionalAssemblies != null)
configuration.AddAssemblies(additionalAssemblies); configuration.AddAssemblies(additionalAssemblies);
//configuration.AddAssemblies(moduleAssemblies); configuration.AddAssemblies(pluginAssemblies);
}); });
@@ -107,7 +115,7 @@ public static class Startup
var config = startupServiceProvider.GetRequiredService<AppConfiguration>(); var config = startupServiceProvider.GetRequiredService<AppConfiguration>();
ApplicationStateHelper.SetConfiguration(config); ApplicationStateHelper.SetConfiguration(config);
// Start the actual app // Start the actual app
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
await ConfigureLogging(builder); await ConfigureLogging(builder);
@@ -118,7 +126,7 @@ public static class Startup
startupServiceProvider.GetRequiredService<IDatabaseStartup[]>() startupServiceProvider.GetRequiredService<IDatabaseStartup[]>()
); );
// Call interfaces // Call interfaces
foreach (var startupInterface in appStartupInterfaces) foreach (var startupInterface in appStartupInterfaces)
{ {
try try
@@ -138,13 +146,16 @@ public static class Startup
var controllerBuilder = builder.Services.AddControllers(); var controllerBuilder = builder.Services.AddControllers();
// Add current assemblies to the application part // Add current assemblies to the application part
//foreach (var moduleAssembly in moduleAssemblies) foreach (var moduleAssembly in pluginAssemblies)
// controllerBuilder.AddApplicationPart(moduleAssembly); controllerBuilder.AddApplicationPart(moduleAssembly);
builder.Services.AddSingleton(config); builder.Services.AddSingleton(config);
builder.Services.AddSingleton(pluginService);
builder.Services.AutoAddServices(typeof(Startup).Assembly); builder.Services.AutoAddServices(typeof(Startup).Assembly);
builder.Services.AddHttpClient(); builder.Services.AddHttpClient();
await ConfigureCaching(builder, startupLogger, config);
await ConfigureOAuth2(builder, startupLogger, config); await ConfigureOAuth2(builder, startupLogger, config);
// Implementation service // Implementation service
@@ -155,10 +166,10 @@ public static class Startup
configuration.AddAssembly(typeof(Startup).Assembly); configuration.AddAssembly(typeof(Startup).Assembly);
if(additionalAssemblies != null) if (additionalAssemblies != null)
configuration.AddAssemblies(additionalAssemblies); configuration.AddAssemblies(additionalAssemblies);
//configuration.AddAssemblies(moduleAssemblies); configuration.AddAssemblies(pluginAssemblies);
}); });
var app = builder.Build(); var app = builder.Build();
@@ -180,7 +191,7 @@ public static class Startup
await UseOAuth2(app); await UseOAuth2(app);
// Call interfaces // Call interfaces
foreach (var startupInterface in appStartupInterfaces) foreach (var startupInterface in appStartupInterfaces)
{ {
try try
@@ -201,7 +212,7 @@ public static class Startup
app.UseMiddleware<AuthorizationMiddleware>(); app.UseMiddleware<AuthorizationMiddleware>();
// Call interfaces // Call interfaces
var endpointStartupInterfaces = startupServiceProvider.GetRequiredService<IEndpointStartup[]>(); var endpointStartupInterfaces = startupServiceProvider.GetRequiredService<IEndpointStartup[]>();
foreach (var endpointStartup in endpointStartupInterfaces) foreach (var endpointStartup in endpointStartupInterfaces)
@@ -347,4 +358,34 @@ public static class Startup
} }
#endregion #endregion
#region Caching
public static Task ConfigureCaching(WebApplicationBuilder builder, ILogger logger, AppConfiguration configuration)
{
builder.Services.AddMemoryCache();
return Task.CompletedTask;
}
#endregion
#region Plugin loading
private static async Task<Assembly[]> LoadPlugins(PluginService pluginService, ILoggerFactory loggerFactory)
{
var pluginLoader = new PluginLoaderService(
loggerFactory.CreateLogger<PluginLoaderService>()
);
var assemblyFiles = pluginService.GetAssemblies("apiServer").Values.ToArray();
var entrypoints = pluginService.GetEntrypoints("apiServer");
pluginLoader.AddFilesSource(assemblyFiles, entrypoints);
await pluginLoader.Load();
return pluginLoader.PluginAssemblies;
}
#endregion
} }

View File

@@ -22,7 +22,7 @@
<PackageReference Include="Blazor-ApexCharts" Version="3.5.0" /> <PackageReference Include="Blazor-ApexCharts" Version="3.5.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.10"/> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.10"/>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.10" PrivateAssets="all"/> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.10" PrivateAssets="all"/>
<PackageReference Include="MoonCore" Version="1.7.4" /> <PackageReference Include="MoonCore" Version="1.7.8" />
<PackageReference Include="MoonCore.Blazor" Version="1.2.7" /> <PackageReference Include="MoonCore.Blazor" Version="1.2.7" />
<PackageReference Include="MoonCore.PluginFramework" Version="1.0.4" /> <PackageReference Include="MoonCore.PluginFramework" Version="1.0.4" />
<PackageReference Include="MoonCore.Blazor.Tailwind" Version="1.1.2" /> <PackageReference Include="MoonCore.Blazor.Tailwind" Version="1.1.2" />

View File

@@ -1,3 +0,0 @@
using Moonlight.Client;
await Startup.Run(args, []);

View File

@@ -9,6 +9,7 @@ using MoonCore.Blazor.Tailwind.Forms.Components;
using MoonCore.Extensions; using MoonCore.Extensions;
using MoonCore.Helpers; using MoonCore.Helpers;
using MoonCore.PluginFramework.Extensions; using MoonCore.PluginFramework.Extensions;
using MoonCore.Plugins;
using Moonlight.Client.Interfaces; using Moonlight.Client.Interfaces;
using Moonlight.Client.Services; using Moonlight.Client.Services;
using Moonlight.Client.UI; using Moonlight.Client.UI;
@@ -18,6 +19,9 @@ namespace Moonlight.Client;
public class Startup public class Startup
{ {
public static async Task Main(string[] args)
=> await Run(args, []);
public static async Task Run(string[] args, Assembly[] assemblies) public static async Task Run(string[] args, Assembly[] assemblies)
{ {
// Build pre run logger // Build pre run logger
@@ -50,6 +54,16 @@ public class Startup
// Building app // Building app
var builder = WebAssemblyHostBuilder.CreateDefault(args); var builder = WebAssemblyHostBuilder.CreateDefault(args);
// Load plugins
var pluginLoader = new PluginLoaderService(
loggerFactory.CreateLogger<PluginLoaderService>()
);
pluginLoader.AddHttpHostedSource($"{builder.HostEnvironment.BaseAddress}api/pluginsStream");
await pluginLoader.Load();
builder.Services.AddSingleton(pluginLoader);
// Configure application logging // Configure application logging
builder.Logging.ClearProviders(); builder.Logging.ClearProviders();
builder.Logging.AddProviders(providers); builder.Logging.AddProviders(providers);
@@ -76,8 +90,11 @@ public class Startup
builder.Services.AddPlugins(configuration => builder.Services.AddPlugins(configuration =>
{ {
configuration.AddAssembly(typeof(Startup).Assembly); configuration.AddAssembly(typeof(Startup).Assembly);
configuration.AddAssemblies(assemblies); configuration.AddAssemblies(assemblies);
configuration.AddAssemblies(pluginLoader.PluginAssemblies);
configuration.AddInterface<IAppLoader>(); configuration.AddInterface<IAppLoader>();
configuration.AddInterface<IAppScreen>(); configuration.AddInterface<IAppScreen>();

View File

@@ -1,9 +1,12 @@
@using Moonlight.Client.UI.Layouts @using Moonlight.Client.UI.Layouts
@using MoonCore.Blazor.Components @using MoonCore.Blazor.Components
@using MoonCore.Plugins
@inject PluginLoaderService PluginLoaderService
<ErrorLogger> <ErrorLogger>
<OAuth2AuthenticationHandler> <OAuth2AuthenticationHandler>
<Router AppAssembly="@typeof(App).Assembly"> <Router AppAssembly="@typeof(App).Assembly" AdditionalAssemblies="PluginLoaderService.PluginAssemblies">
<Found Context="routeData"> <Found Context="routeData">
<CascadingValue Name="TargetPageType" Value="routeData.PageType"> <CascadingValue Name="TargetPageType" Value="routeData.PageType">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/> <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/>