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 IServiceCollection Services { get; set; }
public IServiceProvider Provider { get; set; } public IServiceProvider Provider { get; set; }
public IServiceScope Scope { 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> PreInitTasks = new();
public List<Action> PostInitTasks = new(); public List<Action> PostInitTasks = new();
} }

View file

@ -9,7 +9,7 @@ public class PluginService
{ {
private readonly List<MoonlightPlugin> Plugins = new(); private readonly List<MoonlightPlugin> Plugins = new();
public async Task Load(IServiceCollection services) public async Task Load(WebApplicationBuilder webApplicationBuilder)
{ {
var path = PathBuilder.Dir("storage", "plugins"); var path = PathBuilder.Dir("storage", "plugins");
Directory.CreateDirectory(path); Directory.CreateDirectory(path);
@ -36,7 +36,8 @@ public class PluginService
// Create environment // Create environment
plugin.Context = new PluginContext() plugin.Context = new PluginContext()
{ {
Services = services Services = webApplicationBuilder.Services,
WebApplicationBuilder = webApplicationBuilder
}; };
try 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) foreach (var plugin in Plugins)
{ {
// Pass through the dependency injection // Pass through the dependency injection
var scope = provider.CreateScope(); var scope = webApplication.Services.CreateScope();
plugin.Context.Provider = scope.ServiceProvider; plugin.Context.Provider = scope.ServiceProvider;
plugin.Context.Scope = scope; plugin.Context.Scope = scope;
plugin.Context.WebApplication = webApplication;
Logger.Info($"Running post init tasks for {plugin.GetType().Name}"); 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(); var pluginService = new PluginService();
builder.Services.AddSingleton(pluginService); builder.Services.AddSingleton(pluginService);
await pluginService.Load(builder.Services); await pluginService.Load(builder);
await pluginService.RunPreInit(); await pluginService.RunPreInit();
builder.Services.AddDbContext<DataContext>(); builder.Services.AddDbContext<DataContext>();
@ -106,6 +106,6 @@ app.Services.GetRequiredService<AutoMailSendService>();
var serviceService = app.Services.GetRequiredService<ServiceAdminService>(); var serviceService = app.Services.GetRequiredService<ServiceAdminService>();
await serviceService.RegisterAction(ServiceType.Server, new DummyActions()); await serviceService.RegisterAction(ServiceType.Server, new DummyActions());
await pluginService.RunPrePost(app.Services); await pluginService.RunPrePost(app);
app.Run(); app.Run();