Upgraded mooncore versions. Cleaned up code, especially startup code. Changed versions
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using MoonCore.Blazor.FlyonUi.Exceptions;
|
||||
using MoonCore.Permissions;
|
||||
@@ -7,22 +8,20 @@ using Moonlight.Client.Services;
|
||||
|
||||
namespace Moonlight.Client.Startup;
|
||||
|
||||
public partial class Startup
|
||||
public static partial class Startup
|
||||
{
|
||||
private Task RegisterAuthenticationAsync()
|
||||
private static void AddAuth(this WebAssemblyHostBuilder builder)
|
||||
{
|
||||
WebAssemblyHostBuilder.Services.AddAuthorizationCore();
|
||||
WebAssemblyHostBuilder.Services.AddCascadingAuthenticationState();
|
||||
builder.Services.AddAuthorizationCore();
|
||||
builder.Services.AddCascadingAuthenticationState();
|
||||
|
||||
WebAssemblyHostBuilder.Services.AddScoped<AuthenticationStateProvider, RemoteAuthStateProvider>();
|
||||
WebAssemblyHostBuilder.Services.AddScoped<IGlobalErrorFilter, UnauthenticatedErrorFilter>();
|
||||
builder.Services.AddScoped<AuthenticationStateProvider, RemoteAuthStateProvider>();
|
||||
builder.Services.AddScoped<IGlobalErrorFilter, UnauthenticatedErrorFilter>();
|
||||
|
||||
WebAssemblyHostBuilder.Services.AddAuthorizationPermissions(options =>
|
||||
builder.Services.AddAuthorizationPermissions(options =>
|
||||
{
|
||||
options.ClaimName = "Permissions";
|
||||
options.Prefix = "permissions:";
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -1,39 +1,42 @@
|
||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using MoonCore.Blazor.FlyonUi;
|
||||
using MoonCore.Blazor.FlyonUi.Exceptions;
|
||||
using MoonCore.Extensions;
|
||||
using MoonCore.Helpers;
|
||||
using Moonlight.Client.Implementations;
|
||||
using Moonlight.Client.Services;
|
||||
using Moonlight.Client.UI;
|
||||
|
||||
namespace Moonlight.Client.Startup;
|
||||
|
||||
public partial class Startup
|
||||
public static partial class Startup
|
||||
{
|
||||
private Task RegisterBaseAsync()
|
||||
private static void AddBase(this WebAssemblyHostBuilder builder)
|
||||
{
|
||||
WebAssemblyHostBuilder.RootComponents.Add<App>("#app");
|
||||
WebAssemblyHostBuilder.RootComponents.Add<HeadOutlet>("head::after");
|
||||
builder.RootComponents.Add<App>("#app");
|
||||
builder.RootComponents.Add<HeadOutlet>("head::after");
|
||||
|
||||
WebAssemblyHostBuilder.Services.AddScoped(_ =>
|
||||
builder.Services.AddScoped(_ =>
|
||||
new HttpClient
|
||||
{
|
||||
BaseAddress = new Uri(Configuration.ApiUrl)
|
||||
BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
|
||||
}
|
||||
);
|
||||
|
||||
WebAssemblyHostBuilder.Services.AddScoped(sp =>
|
||||
builder.Services.AddScoped(sp =>
|
||||
{
|
||||
var httpClient = sp.GetRequiredService<HttpClient>();
|
||||
return new HttpApiClient(httpClient);
|
||||
});
|
||||
|
||||
WebAssemblyHostBuilder.Services.AddFileManagerOperations();
|
||||
WebAssemblyHostBuilder.Services.AddFlyonUiServices();
|
||||
builder.Services.AddFileManagerOperations();
|
||||
builder.Services.AddFlyonUiServices();
|
||||
|
||||
WebAssemblyHostBuilder.Services.AddScoped<ThemeService>();
|
||||
builder.Services.AddScoped<ThemeService>();
|
||||
|
||||
WebAssemblyHostBuilder.Services.AutoAddServices<Startup>();
|
||||
|
||||
return Task.CompletedTask;
|
||||
builder.Services.AutoAddServices<IAssemblyMarker>();
|
||||
|
||||
builder.Services.AddScoped<IGlobalErrorFilter, LogErrorFilter>();
|
||||
}
|
||||
}
|
||||
@@ -1,30 +1,13 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using MoonCore.Blazor.FlyonUi.Exceptions;
|
||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||
using MoonCore.Logging;
|
||||
using Moonlight.Client.Implementations;
|
||||
|
||||
namespace Moonlight.Client.Startup;
|
||||
|
||||
public partial class Startup
|
||||
public static partial class Startup
|
||||
{
|
||||
private Task SetupLoggingAsync()
|
||||
private static void AddLogging(this WebAssemblyHostBuilder builder)
|
||||
{
|
||||
var loggerFactory = new LoggerFactory();
|
||||
|
||||
loggerFactory.AddAnsiConsole();
|
||||
|
||||
Logger = loggerFactory.CreateLogger<Startup>();
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task RegisterLoggingAsync()
|
||||
{
|
||||
WebAssemblyHostBuilder.Logging.ClearProviders();
|
||||
WebAssemblyHostBuilder.Logging.AddAnsiConsole();
|
||||
|
||||
WebAssemblyHostBuilder.Services.AddScoped<IGlobalErrorFilter, LogErrorFilter>();
|
||||
|
||||
return Task.CompletedTask;
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Logging.AddAnsiConsole();
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,8 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moonlight.Shared.Misc;
|
||||
|
||||
namespace Moonlight.Client.Startup;
|
||||
|
||||
public partial class Startup
|
||||
public static partial class Startup
|
||||
{
|
||||
private Task PrintVersionAsync()
|
||||
private static void PrintVersion()
|
||||
{
|
||||
// Fancy start console output... yes very fancy :>
|
||||
Console.Write("Running ");
|
||||
@@ -23,30 +19,5 @@ public partial class Startup
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task LoadConfigurationAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
using var httpClient = new HttpClient();
|
||||
httpClient.BaseAddress = new Uri(WebAssemblyHostBuilder.HostEnvironment.BaseAddress);
|
||||
|
||||
var jsonText = await httpClient.GetStringAsync("frontend.json");
|
||||
|
||||
Configuration = JsonSerializer.Deserialize<FrontendConfiguration>(jsonText, new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
})!;
|
||||
|
||||
WebAssemblyHostBuilder.Services.AddSingleton(Configuration);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.LogCritical("Unable to load configuration. Unable to continue: {e}", e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using MoonCore.Logging;
|
||||
using Moonlight.Client.Plugins;
|
||||
using Moonlight.Client.Services;
|
||||
|
||||
@@ -7,72 +7,25 @@ namespace Moonlight.Client.Startup;
|
||||
|
||||
public partial class Startup
|
||||
{
|
||||
private IPluginStartup[] PluginStartups;
|
||||
private IServiceProvider PluginLoadServiceProvider;
|
||||
|
||||
private Task InitializePluginsAsync()
|
||||
private static void AddPlugins(this WebAssemblyHostBuilder builder, IPluginStartup[] startups)
|
||||
{
|
||||
// Define minimal service collection
|
||||
var startupSc = new ServiceCollection();
|
||||
|
||||
// Create logging proxy
|
||||
startupSc.AddLogging(builder =>
|
||||
foreach (var startup in startups)
|
||||
startup.AddPlugin(builder);
|
||||
|
||||
// Get all assemblies and combine them into the application assembly service
|
||||
// TODO: Consider rewriting this as it may not be that performant to do string checking to find distinct items
|
||||
builder.Services.AddSingleton(new ApplicationAssemblyService()
|
||||
{
|
||||
builder.ClearProviders();
|
||||
builder.AddAnsiConsole();
|
||||
});
|
||||
|
||||
PluginLoadServiceProvider = startupSc.BuildServiceProvider();
|
||||
|
||||
// Add application assembly service
|
||||
var appAssemblyService = new ApplicationAssemblyService();
|
||||
|
||||
appAssemblyService.Assemblies.AddRange(
|
||||
PluginStartups
|
||||
Assemblies = startups
|
||||
.Select(x => x.GetType().Assembly)
|
||||
.Distinct()
|
||||
);
|
||||
|
||||
WebAssemblyHostBuilder.Services.AddSingleton(appAssemblyService);
|
||||
|
||||
return Task.CompletedTask;
|
||||
.DistinctBy(x => x.FullName)
|
||||
.ToList()
|
||||
});
|
||||
}
|
||||
|
||||
private async Task HookPluginBuildAsync()
|
||||
private static void ConfigurePlugins(this WebAssemblyHost app, IPluginStartup[] startups)
|
||||
{
|
||||
foreach (var pluginAppStartup in PluginStartups)
|
||||
{
|
||||
try
|
||||
{
|
||||
await pluginAppStartup.BuildApplicationAsync(PluginLoadServiceProvider, WebAssemblyHostBuilder);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.LogError(
|
||||
"An error occured while processing 'BuildApp' for '{name}': {e}",
|
||||
pluginAppStartup.GetType().FullName,
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HookPluginConfigureAsync()
|
||||
{
|
||||
foreach (var pluginAppStartup in PluginStartups)
|
||||
{
|
||||
try
|
||||
{
|
||||
await pluginAppStartup.ConfigureApplicationAsync(PluginLoadServiceProvider, WebAssemblyHost);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.LogError(
|
||||
"An error occured while processing 'ConfigureApp' for '{name}': {e}",
|
||||
pluginAppStartup.GetType().FullName,
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
foreach (var startup in startups)
|
||||
startup.ConfigurePlugin(app);
|
||||
}
|
||||
}
|
||||
@@ -1,48 +1,22 @@
|
||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||
using Moonlight.Client.Plugins;
|
||||
using Moonlight.Shared.Misc;
|
||||
|
||||
namespace Moonlight.Client.Startup;
|
||||
|
||||
public partial class Startup
|
||||
public static partial class Startup
|
||||
{
|
||||
public ILogger<Startup> Logger { get; private set; }
|
||||
|
||||
// WebAssemblyHost
|
||||
public WebAssemblyHostBuilder WebAssemblyHostBuilder { get; private set; }
|
||||
public WebAssemblyHost WebAssemblyHost { get; private set; }
|
||||
|
||||
// Configuration
|
||||
public FrontendConfiguration Configuration { get; private set; }
|
||||
|
||||
|
||||
public Task InitializeAsync(IPluginStartup[]? plugins = null)
|
||||
public static void AddMoonlight(this WebAssemblyHostBuilder builder, IPluginStartup[] startups)
|
||||
{
|
||||
PluginStartups = plugins ?? [];
|
||||
|
||||
return Task.CompletedTask;
|
||||
PrintVersion();
|
||||
|
||||
builder.AddLogging();
|
||||
builder.AddBase();
|
||||
builder.AddAuth();
|
||||
builder.AddPlugins(startups);
|
||||
}
|
||||
|
||||
public async Task AddMoonlightAsync(WebAssemblyHostBuilder builder)
|
||||
public static void ConfigureMoonlight(this WebAssemblyHost app, IPluginStartup[] startups)
|
||||
{
|
||||
WebAssemblyHostBuilder = builder;
|
||||
|
||||
await PrintVersionAsync();
|
||||
|
||||
await SetupLoggingAsync();
|
||||
await LoadConfigurationAsync();
|
||||
await InitializePluginsAsync();
|
||||
|
||||
await RegisterLoggingAsync();
|
||||
await RegisterBaseAsync();
|
||||
await RegisterAuthenticationAsync();
|
||||
await HookPluginBuildAsync();
|
||||
}
|
||||
|
||||
public async Task AddMoonlightAsync(WebAssemblyHost assemblyHost)
|
||||
{
|
||||
WebAssemblyHost = assemblyHost;
|
||||
|
||||
await HookPluginConfigureAsync();
|
||||
app.ConfigurePlugins(startups);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user