Added app startup for the frontend

This commit is contained in:
2024-12-13 18:46:10 +01:00
parent 5efeb5fba6
commit 094af845a0
2 changed files with 80 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
namespace Moonlight.Client.Interfaces;
public interface IAppStartup
{
public Task BuildApp(WebAssemblyHostBuilder builder);
public Task ConfigureApp(WebAssemblyHost app);
}

View File

@@ -36,6 +36,8 @@ public class Startup
// Plugin Loading
private PluginLoaderService PluginLoaderService;
private ApplicationAssemblyService ApplicationAssemblyService;
private IAppStartup[] PluginAppStartups;
public async Task Run(string[] args, Assembly[]? assemblies = null)
{
@@ -53,15 +55,18 @@ public class Startup
await CreateWebAssemblyHostBuilder();
await LoadPlugins();
await InitializePlugins();
await RegisterLogging();
await RegisterBase();
await RegisterOAuth2();
await RegisterFormComponents();
await RegisterInterfaces();
await HookPluginBuild();
await BuildWebAssemblyHost();
await HookPluginConfigure();
await LoadAssets();
await WebAssemblyHost.RunAsync();
@@ -185,6 +190,72 @@ public class Startup
WebAssemblyHostBuilder.Services.AddSingleton(ApplicationAssemblyService);
}
private Task InitializePlugins()
{
var initialisationServiceCollection = new ServiceCollection();
initialisationServiceCollection.AddLogging(builder => { builder.AddProviders(LoggerProviders); });
// Configure plugin loading by using the interface service
initialisationServiceCollection.AddInterfaces(configuration =>
{
// We use moonlight itself as a plugin assembly
configuration.AddAssembly(typeof(Startup).Assembly);
configuration.AddAssemblies(PluginLoaderService.PluginAssemblies);
configuration.AddInterface<IAppStartup>();
});
var initialisationServiceProvider = initialisationServiceCollection.BuildServiceProvider();
PluginAppStartups = initialisationServiceProvider.GetRequiredService<IAppStartup[]>();
return Task.CompletedTask;
}
#region Hooks
private async Task HookPluginBuild()
{
foreach (var pluginAppStartup in PluginAppStartups)
{
try
{
await pluginAppStartup.BuildApp(WebAssemblyHostBuilder);
}
catch (Exception e)
{
Logger.LogError(
"An error occured while processing 'BuildApp' for '{name}': {e}",
pluginAppStartup.GetType().FullName,
e
);
}
}
}
private async Task HookPluginConfigure()
{
foreach (var pluginAppStartup in PluginAppStartups)
{
try
{
await pluginAppStartup.ConfigureApp(WebAssemblyHost);
}
catch (Exception e)
{
Logger.LogError(
"An error occured while processing 'ConfigureApp' for '{name}': {e}",
pluginAppStartup.GetType().FullName,
e
);
}
}
}
#endregion
#endregion
#region Logging