Extended plugin system to support kestrel pipeline injection

This commit is contained in:
Marcel Baumgartner 2023-10-24 12:26:29 +02:00
parent d0f03a19a2
commit aa150a7a69
3 changed files with 10 additions and 6 deletions

View file

@ -5,6 +5,8 @@ public class PluginContext
public IServiceCollection Services { get; set; }
public IServiceProvider Provider { get; set; }
public IServiceScope Scope { get; set; }
public WebApplicationBuilder WebApplicationBuilder { get; set; }
public WebApplication WebApplication { get; set; }
public List<Action> PreInitTasks = new();
public List<Action> PostInitTasks = new();
}

View file

@ -9,7 +9,7 @@ public class PluginService
{
private readonly List<MoonlightPlugin> Plugins = new();
public async Task Load(IServiceCollection services)
public async Task Load(WebApplicationBuilder webApplicationBuilder)
{
var path = PathBuilder.Dir("storage", "plugins");
Directory.CreateDirectory(path);
@ -36,7 +36,8 @@ public class PluginService
// Create environment
plugin.Context = new PluginContext()
{
Services = services
Services = webApplicationBuilder.Services,
WebApplicationBuilder = webApplicationBuilder
};
try
@ -87,14 +88,15 @@ public class PluginService
}
}
public async Task RunPrePost(IServiceProvider provider)
public async Task RunPrePost(WebApplication webApplication)
{
foreach (var plugin in Plugins)
{
// Pass through the dependency injection
var scope = provider.CreateScope();
var scope = webApplication.Services.CreateScope();
plugin.Context.Provider = scope.ServiceProvider;
plugin.Context.Scope = scope;
plugin.Context.WebApplication = webApplication;
Logger.Info($"Running post init tasks for {plugin.GetType().Name}");

View file

@ -33,7 +33,7 @@ var builder = WebApplication.CreateBuilder(args);
var pluginService = new PluginService();
builder.Services.AddSingleton(pluginService);
await pluginService.Load(builder.Services);
await pluginService.Load(builder);
await pluginService.RunPreInit();
builder.Services.AddDbContext<DataContext>();
@ -106,6 +106,6 @@ app.Services.GetRequiredService<AutoMailSendService>();
var serviceService = app.Services.GetRequiredService<ServiceAdminService>();
await serviceService.RegisterAction(ServiceType.Server, new DummyActions());
await pluginService.RunPrePost(app.Services);
await pluginService.RunPrePost(app);
app.Run();