Implementing plugin loading for api server and client
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
11
Moonlight.ApiServer/Models/PluginManifest.cs
Normal file
11
Moonlight.ApiServer/Models/PluginManifest.cs
Normal 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();
|
||||
}
|
||||
@@ -1,12 +1,7 @@
|
||||
namespace Moonlight.ApiServer.Models;
|
||||
namespace Moonlight.ApiServer.Models;
|
||||
|
||||
public class PluginMeta
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string Name { 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();
|
||||
public PluginManifest Manifest { get; set; }
|
||||
public string Path { get; set; }
|
||||
}
|
||||
@@ -23,7 +23,7 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</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.PluginFramework" Version="1.0.4" />
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
|
||||
@@ -38,7 +38,9 @@
|
||||
|
||||
<ItemGroup>
|
||||
<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>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json;
|
||||
using MoonCore.Helpers;
|
||||
using Moonlight.ApiServer.Models;
|
||||
|
||||
@@ -6,10 +6,10 @@ namespace Moonlight.ApiServer.Services;
|
||||
|
||||
public class PluginService
|
||||
{
|
||||
private readonly Dictionary<string, PluginMeta> Plugins = new();
|
||||
private readonly ILogger<PluginService> Logger;
|
||||
public readonly List<PluginMeta> Plugins = new();
|
||||
|
||||
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)
|
||||
{
|
||||
@@ -18,58 +18,91 @@ public class PluginService
|
||||
|
||||
public async Task Load()
|
||||
{
|
||||
Logger.LogInformation("Loading plugins...");
|
||||
|
||||
foreach (var pluginPath in Directory.EnumerateDirectories(PluginsPath))
|
||||
// Load all manifest files
|
||||
foreach (var pluginFolder in Directory.EnumerateDirectories(PluginsFolder))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
var metaContent = await File.ReadAllTextAsync(metaPath);
|
||||
PluginMeta meta;
|
||||
PluginManifest manifest;
|
||||
|
||||
try
|
||||
{
|
||||
meta = JsonSerializer.Deserialize<PluginMeta>(metaContent)
|
||||
?? throw new JsonException();
|
||||
var manifestText = await File.ReadAllTextAsync(manifestPath);
|
||||
manifest = JsonSerializer.Deserialize<PluginManifest>(manifestText)!;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.LogCritical("Unable to deserialize meta.json: {e}", e);
|
||||
continue;
|
||||
Logger.LogError("An unhandled error occured while loading plugin manifest in '{folder}': {e}",
|
||||
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
|
||||
.Values
|
||||
.Where(x => x.Binaries.ContainsKey(section))
|
||||
.ToDictionary(x => x.Id, x => x.Binaries[section]);
|
||||
var pathMappings = new Dictionary<string, string>();
|
||||
|
||||
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 Task.FromResult<Stream?>(null);
|
||||
|
||||
var pluginData = Plugins.First(x => x.Value.Id == plugin);
|
||||
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);
|
||||
return Plugins
|
||||
.Where(x => x.Manifest.Entrypoints.ContainsKey(section))
|
||||
.SelectMany(x => x.Manifest.Entrypoints[section])
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ using MoonCore.Extended.OAuth2.LocalProvider.Implementations;
|
||||
using MoonCore.Extensions;
|
||||
using MoonCore.Helpers;
|
||||
using MoonCore.PluginFramework.Extensions;
|
||||
using MoonCore.Plugins;
|
||||
using Moonlight.ApiServer.Configuration;
|
||||
using Moonlight.ApiServer.Database.Entities;
|
||||
using Moonlight.ApiServer.Helpers;
|
||||
@@ -51,7 +52,7 @@ public static class Startup
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
// Storage i guess
|
||||
// Storage i guess
|
||||
Directory.CreateDirectory(PathBuilder.Dir("storage"));
|
||||
|
||||
// Configure startup logger
|
||||
@@ -69,7 +70,14 @@ public static class 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
|
||||
var startupServiceCollection = new ServiceCollection();
|
||||
@@ -94,10 +102,10 @@ public static class Startup
|
||||
// Configure assemblies to scan
|
||||
configuration.AddAssembly(typeof(Startup).Assembly);
|
||||
|
||||
if(additionalAssemblies != null)
|
||||
if (additionalAssemblies != null)
|
||||
configuration.AddAssemblies(additionalAssemblies);
|
||||
|
||||
//configuration.AddAssemblies(moduleAssemblies);
|
||||
configuration.AddAssemblies(pluginAssemblies);
|
||||
});
|
||||
|
||||
|
||||
@@ -107,7 +115,7 @@ public static class Startup
|
||||
var config = startupServiceProvider.GetRequiredService<AppConfiguration>();
|
||||
ApplicationStateHelper.SetConfiguration(config);
|
||||
|
||||
// Start the actual app
|
||||
// Start the actual app
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
await ConfigureLogging(builder);
|
||||
@@ -118,7 +126,7 @@ public static class Startup
|
||||
startupServiceProvider.GetRequiredService<IDatabaseStartup[]>()
|
||||
);
|
||||
|
||||
// Call interfaces
|
||||
// Call interfaces
|
||||
foreach (var startupInterface in appStartupInterfaces)
|
||||
{
|
||||
try
|
||||
@@ -138,13 +146,16 @@ public static class Startup
|
||||
var controllerBuilder = builder.Services.AddControllers();
|
||||
|
||||
// Add current assemblies to the application part
|
||||
//foreach (var moduleAssembly in moduleAssemblies)
|
||||
// controllerBuilder.AddApplicationPart(moduleAssembly);
|
||||
foreach (var moduleAssembly in pluginAssemblies)
|
||||
controllerBuilder.AddApplicationPart(moduleAssembly);
|
||||
|
||||
builder.Services.AddSingleton(config);
|
||||
builder.Services.AddSingleton(pluginService);
|
||||
builder.Services.AutoAddServices(typeof(Startup).Assembly);
|
||||
builder.Services.AddHttpClient();
|
||||
|
||||
await ConfigureCaching(builder, startupLogger, config);
|
||||
|
||||
await ConfigureOAuth2(builder, startupLogger, config);
|
||||
|
||||
// Implementation service
|
||||
@@ -155,10 +166,10 @@ public static class Startup
|
||||
|
||||
configuration.AddAssembly(typeof(Startup).Assembly);
|
||||
|
||||
if(additionalAssemblies != null)
|
||||
if (additionalAssemblies != null)
|
||||
configuration.AddAssemblies(additionalAssemblies);
|
||||
|
||||
//configuration.AddAssemblies(moduleAssemblies);
|
||||
configuration.AddAssemblies(pluginAssemblies);
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
@@ -180,7 +191,7 @@ public static class Startup
|
||||
|
||||
await UseOAuth2(app);
|
||||
|
||||
// Call interfaces
|
||||
// Call interfaces
|
||||
foreach (var startupInterface in appStartupInterfaces)
|
||||
{
|
||||
try
|
||||
@@ -201,7 +212,7 @@ public static class Startup
|
||||
|
||||
app.UseMiddleware<AuthorizationMiddleware>();
|
||||
|
||||
// Call interfaces
|
||||
// Call interfaces
|
||||
var endpointStartupInterfaces = startupServiceProvider.GetRequiredService<IEndpointStartup[]>();
|
||||
|
||||
foreach (var endpointStartup in endpointStartupInterfaces)
|
||||
@@ -347,4 +358,34 @@ public static class Startup
|
||||
}
|
||||
|
||||
#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
|
||||
}
|
||||
@@ -22,7 +22,7 @@
|
||||
<PackageReference Include="Blazor-ApexCharts" Version="3.5.0" />
|
||||
<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="MoonCore" Version="1.7.4" />
|
||||
<PackageReference Include="MoonCore" Version="1.7.8" />
|
||||
<PackageReference Include="MoonCore.Blazor" Version="1.2.7" />
|
||||
<PackageReference Include="MoonCore.PluginFramework" Version="1.0.4" />
|
||||
<PackageReference Include="MoonCore.Blazor.Tailwind" Version="1.1.2" />
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
using Moonlight.Client;
|
||||
|
||||
await Startup.Run(args, []);
|
||||
@@ -9,6 +9,7 @@ using MoonCore.Blazor.Tailwind.Forms.Components;
|
||||
using MoonCore.Extensions;
|
||||
using MoonCore.Helpers;
|
||||
using MoonCore.PluginFramework.Extensions;
|
||||
using MoonCore.Plugins;
|
||||
using Moonlight.Client.Interfaces;
|
||||
using Moonlight.Client.Services;
|
||||
using Moonlight.Client.UI;
|
||||
@@ -18,6 +19,9 @@ namespace Moonlight.Client;
|
||||
|
||||
public class Startup
|
||||
{
|
||||
public static async Task Main(string[] args)
|
||||
=> await Run(args, []);
|
||||
|
||||
public static async Task Run(string[] args, Assembly[] assemblies)
|
||||
{
|
||||
// Build pre run logger
|
||||
@@ -50,6 +54,16 @@ public class Startup
|
||||
// Building app
|
||||
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
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Logging.AddProviders(providers);
|
||||
@@ -76,8 +90,11 @@ public class Startup
|
||||
builder.Services.AddPlugins(configuration =>
|
||||
{
|
||||
configuration.AddAssembly(typeof(Startup).Assembly);
|
||||
|
||||
configuration.AddAssemblies(assemblies);
|
||||
|
||||
configuration.AddAssemblies(pluginLoader.PluginAssemblies);
|
||||
|
||||
configuration.AddInterface<IAppLoader>();
|
||||
configuration.AddInterface<IAppScreen>();
|
||||
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
@using Moonlight.Client.UI.Layouts
|
||||
@using MoonCore.Blazor.Components
|
||||
@using MoonCore.Plugins
|
||||
|
||||
@inject PluginLoaderService PluginLoaderService
|
||||
|
||||
<ErrorLogger>
|
||||
<OAuth2AuthenticationHandler>
|
||||
<Router AppAssembly="@typeof(App).Assembly">
|
||||
<Router AppAssembly="@typeof(App).Assembly" AdditionalAssemblies="PluginLoaderService.PluginAssemblies">
|
||||
<Found Context="routeData">
|
||||
<CascadingValue Name="TargetPageType" Value="routeData.PageType">
|
||||
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/>
|
||||
|
||||
Reference in New Issue
Block a user