Upgraded packages. Improved startup. Removed unused components

This commit is contained in:
Masu-Baumgartner
2024-11-20 17:21:17 +01:00
parent 2d0a0e53c0
commit fe31c01a06
12 changed files with 598 additions and 357 deletions

View File

@@ -1,12 +0,0 @@
using System.Reflection;
namespace Moonlight.Client;
public class DevClient
{
public async static Task Run(string[] args, Assembly[] assemblies)
{
Console.WriteLine("Preparing development client");
await Startup.Run(args, assemblies);
}
}

View File

@@ -24,8 +24,8 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.10" PrivateAssets="all"/>
<PackageReference Include="MoonCore" Version="1.7.8" />
<PackageReference Include="MoonCore.Blazor" Version="1.2.7" />
<PackageReference Include="MoonCore.PluginFramework" Version="1.0.4" />
<PackageReference Include="MoonCore.Blazor.Tailwind" Version="1.1.2" />
<PackageReference Include="MoonCore.PluginFramework" Version="1.0.5" />
<PackageReference Include="MoonCore.Blazor.Tailwind" Version="1.1.4" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,13 @@
using Moonlight.Client;
var startup = new Startup();
try
{
await startup.Run(args);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}

View File

@@ -19,22 +19,46 @@ namespace Moonlight.Client;
public class Startup
{
public static async Task Main(string[] args)
=> await Run(args, []);
public static async Task Run(string[] args, Assembly[] assemblies)
private string[] Args;
private Assembly[] AdditionalAssemblies;
// Logging
private ILoggerProvider[] LoggerProviders;
private ILoggerFactory LoggerFactory;
private ILogger<Startup> Logger;
// WebAssemblyHost
private WebAssemblyHostBuilder WebAssemblyHostBuilder;
private WebAssemblyHost WebAssemblyHost;
// Plugin Loading
private PluginLoaderService PluginLoaderService;
public async Task Run(string[] args, Assembly[]? assemblies = null)
{
// Build pre run logger
var providers = LoggerBuildHelper.BuildFromConfiguration(configuration =>
{
configuration.Console.Enable = true;
configuration.Console.EnableAnsiMode = true;
configuration.FileLogging.Enable = false;
});
Args = args;
AdditionalAssemblies = assemblies ?? [];
using var loggerFactory = new LoggerFactory(providers);
var logger = loggerFactory.CreateLogger("Startup");
await PrintVersion();
await SetupLogging();
await CreateWebAssemblyHostBuilder();
await LoadPlugins();
await RegisterLogging();
await RegisterBase();
await RegisterOAuth2();
await RegisterFormComponents();
await RegisterInterfaces();
await BuildWebAssemblyHost();
await WebAssemblyHost.RunAsync();
}
private Task PrintVersion()
{
// Fancy start console output... yes very fancy :>
Console.Write("Running ");
@@ -50,59 +74,133 @@ public class Startup
}
Console.WriteLine();
// Building app
var builder = WebAssemblyHostBuilder.CreateDefault(args);
// Load plugins
var pluginLoader = new PluginLoaderService(
loggerFactory.CreateLogger<PluginLoaderService>()
return Task.CompletedTask;
}
private Task RegisterBase()
{
WebAssemblyHostBuilder.RootComponents.Add<App>("#app");
WebAssemblyHostBuilder.RootComponents.Add<HeadOutlet>("head::after");
WebAssemblyHostBuilder.Services.AddScoped(_ =>
new HttpClient
{
BaseAddress = new Uri(WebAssemblyHostBuilder.HostEnvironment.BaseAddress)
}
);
pluginLoader.AddHttpHostedSource($"{builder.HostEnvironment.BaseAddress}api/pluginsStream");
await pluginLoader.Load();
WebAssemblyHostBuilder.Services.AddScoped<WindowService>();
WebAssemblyHostBuilder.Services.AddMoonCoreBlazorTailwind();
WebAssemblyHostBuilder.Services.AddScoped<LocalStorageService>();
builder.Services.AddSingleton(pluginLoader);
WebAssemblyHostBuilder.Services.AutoAddServices<Program>();
// Configure application logging
builder.Logging.ClearProviders();
builder.Logging.AddProviders(providers);
return Task.CompletedTask;
}
private Task RegisterOAuth2()
{
WebAssemblyHostBuilder.AddTokenAuthentication();
WebAssemblyHostBuilder.AddOAuth2();
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.Services.AddScoped(_ => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.AddTokenAuthentication();
builder.AddOAuth2();
builder.Services.AddMoonCoreBlazorTailwind();
builder.Services.AddScoped<WindowService>();
builder.Services.AddScoped<LocalStorageService>();
builder.Services.AutoAddServices<Startup>();
return Task.CompletedTask;
}
private Task RegisterFormComponents()
{
FormComponentRepository.Set<string, StringComponent>();
FormComponentRepository.Set<int, IntComponent>();
FormComponentRepository.Set<DateTime, DateComponent>();
return Task.CompletedTask;
}
// Interface service
builder.Services.AddPlugins(configuration =>
#region Interfaces
private Task RegisterInterfaces()
{
WebAssemblyHostBuilder.Services.AddInterfaces(configuration =>
{
// We use moonlight itself as a plugin assembly
configuration.AddAssembly(typeof(Startup).Assembly);
configuration.AddAssemblies(assemblies);
configuration.AddAssemblies(pluginLoader.PluginAssemblies);
configuration.AddAssemblies(AdditionalAssemblies);
configuration.AddAssemblies(PluginLoaderService.PluginAssemblies);
configuration.AddInterface<IAppLoader>();
configuration.AddInterface<IAppScreen>();
configuration.AddInterface<ISidebarItemProvider>();
});
var app = builder.Build();
await app.RunAsync();
return Task.CompletedTask;
}
#endregion
#region Plugins
private async Task LoadPlugins()
{
// Initialize api server plugin loader
PluginLoaderService = new PluginLoaderService(
LoggerFactory.CreateLogger<PluginLoaderService>()
);
// Build source from the retrieved data
var pluginsStreamUrl = $"{WebAssemblyHostBuilder.HostEnvironment.BaseAddress}api/pluginsStream";
PluginLoaderService.AddHttpHostedSource(pluginsStreamUrl);
// Perform assembly loading
await PluginLoaderService.Load();
// Add plugin loader service to di for the Router/App.razor
WebAssemblyHostBuilder.Services.AddSingleton(PluginLoaderService);
}
#endregion
#region Logging
private Task SetupLogging()
{
LoggerProviders = LoggerBuildHelper.BuildFromConfiguration(configuration =>
{
configuration.Console.Enable = true;
configuration.Console.EnableAnsiMode = true;
configuration.FileLogging.Enable = false;
});
LoggerFactory = new LoggerFactory();
LoggerFactory.AddProviders(LoggerProviders);
Logger = LoggerFactory.CreateLogger<Startup>();
return Task.CompletedTask;
}
private Task RegisterLogging()
{
WebAssemblyHostBuilder.Logging.ClearProviders();
WebAssemblyHostBuilder.Logging.AddProviders(LoggerProviders);
return Task.CompletedTask;
}
#endregion
#region Web Application
private Task CreateWebAssemblyHostBuilder()
{
WebAssemblyHostBuilder = WebAssemblyHostBuilder.CreateDefault(Args);
return Task.CompletedTask;
}
private Task BuildWebAssemblyHost()
{
WebAssemblyHost = WebAssemblyHostBuilder.Build();
return Task.CompletedTask;
}
#endregion
}

View File

@@ -399,5 +399,14 @@
"whitespace-nowrap",
"z-10",
"z-40",
"z-50"
"z-50",
"sm:max-w-md",
"sm:max-w-lg",
"sm:max-w-xl",
"sm:max-w-2xl",
"sm:max-w-3xl",
"sm:max-w-4xl",
"sm:max-w-5xl",
"sm:max-w-6xl",
"sm:max-w-7xl"
]