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);
}
}