From 2b62fc141d0671d2a59ee04248d82471fe00f7a4 Mon Sep 17 00:00:00 2001 From: ChiaraBm Date: Mon, 14 Jul 2025 19:25:08 +0200 Subject: [PATCH] Refactored startup. Updated usings. Removed dockerignore --- .dockerignore | 30 ---- Moonlight.ApiServer.Runtime/Program.cs | 25 +++- Moonlight.ApiServer/CleanStartup.cs | 132 ++++++++++++++++++ .../Admin/Sys/AdvancedController.cs | 1 + .../Admin/Sys/DiagnoseController.cs | 1 + .../Http/Controllers/Auth/AuthController.cs | 1 + .../Frontend/FrontendController.cs | 1 + .../Controllers/OAuth2/OAuth2Controller.cs | 1 + .../Implementations/LocalOAuth2Provider.cs | 1 + .../Metrics/ApplicationMetric.cs | 1 + .../Implementations/Metrics/UsersMetric.cs | 1 + .../Implementations/Startup/CoreStartup.cs | 4 + .../Moonlight.ApiServer.csproj | 9 +- Moonlight.ApiServer/Plugins/IPluginStartup.cs | 4 + Moonlight.ApiServer/Program.cs | 5 - .../Services/ApplicationService.cs | 2 + .../Services/DiagnoseService.cs | 1 + .../Services/FrontendService.cs | 1 + .../Services/MetricsBackgroundService.cs | 3 + Moonlight.ApiServer/Startup.cs | 9 +- Moonlight.ApiServer/Startup/CleanStartup.cs | 66 +++++++++ Moonlight.ApiServer/Startup/Startup.Auth.cs | 6 + Moonlight.ApiServer/Startup/Startup.Base.cs | 74 ++++++++++ Moonlight.ApiServer/Startup/Startup.Config.cs | 6 + .../Startup/Startup.Database.cs | 6 + .../Startup/Startup.Hangfire.cs | 6 + .../Startup/Startup.Logging.cs | 6 + Moonlight.ApiServer/Startup/Startup.Misc.cs | 6 + .../Startup/Startup.Plugins.cs | 89 ++++++++++++ 29 files changed, 450 insertions(+), 48 deletions(-) delete mode 100644 .dockerignore create mode 100644 Moonlight.ApiServer/CleanStartup.cs delete mode 100644 Moonlight.ApiServer/Program.cs create mode 100644 Moonlight.ApiServer/Startup/CleanStartup.cs create mode 100644 Moonlight.ApiServer/Startup/Startup.Auth.cs create mode 100644 Moonlight.ApiServer/Startup/Startup.Base.cs create mode 100644 Moonlight.ApiServer/Startup/Startup.Config.cs create mode 100644 Moonlight.ApiServer/Startup/Startup.Database.cs create mode 100644 Moonlight.ApiServer/Startup/Startup.Hangfire.cs create mode 100644 Moonlight.ApiServer/Startup/Startup.Logging.cs create mode 100644 Moonlight.ApiServer/Startup/Startup.Misc.cs create mode 100644 Moonlight.ApiServer/Startup/Startup.Plugins.cs diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index 2a8688fc..00000000 --- a/.dockerignore +++ /dev/null @@ -1,30 +0,0 @@ -**/.dockerignore -**/.env -**/.git -**/.gitignore -**/.project -**/.settings -**/.toolstarget -**/.vs -**/.vscode -**/.idea -**/*.*proj.user -**/*.dbmdl -**/*.jfm -**/azds.yaml -**/bin -**/charts -**/docker-compose* -**/Dockerfile* -**/node_modules -**/npm-debug.log -**/obj -**/secrets.dev.yaml -**/values.dev.yaml -**/storage -**/compose.yml -LICENSE -README.md - -# For the people who run moonlight inside the main repo with the relative data path -data diff --git a/Moonlight.ApiServer.Runtime/Program.cs b/Moonlight.ApiServer.Runtime/Program.cs index 619c343a..a2311356 100644 --- a/Moonlight.ApiServer.Runtime/Program.cs +++ b/Moonlight.ApiServer.Runtime/Program.cs @@ -1,9 +1,28 @@ using Moonlight.ApiServer; using Moonlight.ApiServer.Runtime; -var startup = new Startup(); - var pluginLoader = new PluginLoader(); pluginLoader.Initialize(); +/* +await startup.Run(args, pluginLoader.Instances); -await startup.Run(args, pluginLoader.Instances); \ No newline at end of file +*/ + +var cs = new CleanStartup(); + +var builder = WebApplication.CreateBuilder(); + +await cs.AddMoonlight(builder, args, pluginLoader.Instances); + +var app = builder.Build(); + +await cs.AddMoonlight(app, args, pluginLoader.Instances); + +if (app.Environment.IsDevelopment()) + app.UseWebAssemblyDebugging(); + +app.UseBlazorFrameworkFiles(); +app.UseStaticFiles(); + + +await app.RunAsync(); \ No newline at end of file diff --git a/Moonlight.ApiServer/CleanStartup.cs b/Moonlight.ApiServer/CleanStartup.cs new file mode 100644 index 00000000..d3a72d27 --- /dev/null +++ b/Moonlight.ApiServer/CleanStartup.cs @@ -0,0 +1,132 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.Logging; +using Moonlight.ApiServer.Configuration; +using Moonlight.ApiServer.Plugins; + +namespace Moonlight.ApiServer; + +public class CleanStartup +{ + // Logger + private ILogger Logger; + + // Configuration + private AppConfiguration Configuration; + + // WebApplication Stuff + private WebApplication WebApplication; + private WebApplicationBuilder WebApplicationBuilder; + + public async Task AddMoonlight( + WebApplicationBuilder builder, + string[] args, + IPluginStartup[]? plugins = null + ) + { + + } + + public async Task AddMoonlight( + WebApplication application, + string[] args, + IPluginStartup[]? plugins = null + ) + { + } + + #region Misc + + private Task PrintVersion() + { + // Fancy start console output... yes very fancy :> + var rainbow = new Crayon.Rainbow(0.5); + foreach (var c in "Moonlight") + { + Console.Write( + rainbow + .Next() + .Bold() + .Text(c.ToString()) + ); + } + + Console.WriteLine(); + + return Task.CompletedTask; + } + + private Task CreateStorage() + { + Directory.CreateDirectory("storage"); + Directory.CreateDirectory(Path.Combine("storage", "logs")); + + return Task.CompletedTask; + } + + #endregion + + #region Base + + private Task RegisterBase() + { + WebApplicationBuilder.Services.AutoAddServices(); + WebApplicationBuilder.Services.AddHttpClient(); + + WebApplicationBuilder.Services.AddApiExceptionHandler(); + + // Add pre-existing services + WebApplicationBuilder.Services.AddSingleton(Configuration); + + // Configure controllers + var mvcBuilder = WebApplicationBuilder.Services.AddControllers(); + + // Add plugin assemblies as application parts + foreach (var pluginStartup in PluginStartups.Select(x => x.GetType().Assembly).Distinct()) + mvcBuilder.AddApplicationPart(pluginStartup.GetType().Assembly); + + return Task.CompletedTask; + } + + private Task UseBase() + { + WebApplication.UseRouting(); + WebApplication.UseExceptionHandler(); + + if (Configuration.Client.Enable) + { + if (WebApplication.Environment.IsDevelopment()) + WebApplication.UseWebAssemblyDebugging(); + + WebApplication.UseBlazorFrameworkFiles(); + WebApplication.UseStaticFiles(); + } + + return Task.CompletedTask; + } + + private Task MapBase() + { + WebApplication.MapControllers(); + + if (Configuration.Client.Enable) + WebApplication.MapFallbackToController("Index", "Frontend"); + + return Task.CompletedTask; + } + + private Task ConfigureKestrel() + { + WebApplicationBuilder.WebHost.ConfigureKestrel(kestrelOptions => + { + var maxUploadInBytes = ByteConverter + .FromMegaBytes(Configuration.Kestrel.UploadLimit) + .Bytes; + + kestrelOptions.Limits.MaxRequestBodySize = maxUploadInBytes; + }); + + return Task.CompletedTask; + } + + #endregion +} \ No newline at end of file diff --git a/Moonlight.ApiServer/Http/Controllers/Admin/Sys/AdvancedController.cs b/Moonlight.ApiServer/Http/Controllers/Admin/Sys/AdvancedController.cs index 1184da4c..3873564f 100644 --- a/Moonlight.ApiServer/Http/Controllers/Admin/Sys/AdvancedController.cs +++ b/Moonlight.ApiServer/Http/Controllers/Admin/Sys/AdvancedController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Moonlight.ApiServer.Services; diff --git a/Moonlight.ApiServer/Http/Controllers/Admin/Sys/DiagnoseController.cs b/Moonlight.ApiServer/Http/Controllers/Admin/Sys/DiagnoseController.cs index fb4d3a58..8b2bbc51 100644 --- a/Moonlight.ApiServer/Http/Controllers/Admin/Sys/DiagnoseController.cs +++ b/Moonlight.ApiServer/Http/Controllers/Admin/Sys/DiagnoseController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Moonlight.ApiServer.Services; using Moonlight.Shared.Http.Requests.Admin.Sys; diff --git a/Moonlight.ApiServer/Http/Controllers/Auth/AuthController.cs b/Moonlight.ApiServer/Http/Controllers/Auth/AuthController.cs index 61de0a4f..096d198c 100644 --- a/Moonlight.ApiServer/Http/Controllers/Auth/AuthController.cs +++ b/Moonlight.ApiServer/Http/Controllers/Auth/AuthController.cs @@ -4,6 +4,7 @@ using System.Text.Json; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; using Microsoft.IdentityModel.Tokens; using MoonCore.Exceptions; using MoonCore.Extended.Abstractions; diff --git a/Moonlight.ApiServer/Http/Controllers/Frontend/FrontendController.cs b/Moonlight.ApiServer/Http/Controllers/Frontend/FrontendController.cs index 028971c6..9271af67 100644 --- a/Moonlight.ApiServer/Http/Controllers/Frontend/FrontendController.cs +++ b/Moonlight.ApiServer/Http/Controllers/Frontend/FrontendController.cs @@ -1,4 +1,5 @@ using System.Text; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Moonlight.ApiServer.Services; using Moonlight.Shared.Misc; diff --git a/Moonlight.ApiServer/Http/Controllers/OAuth2/OAuth2Controller.cs b/Moonlight.ApiServer/Http/Controllers/OAuth2/OAuth2Controller.cs index 50d359aa..18e8ca4a 100644 --- a/Moonlight.ApiServer/Http/Controllers/OAuth2/OAuth2Controller.cs +++ b/Moonlight.ApiServer/Http/Controllers/OAuth2/OAuth2Controller.cs @@ -4,6 +4,7 @@ using System.Security.Claims; using System.Text; using System.Text.RegularExpressions; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; diff --git a/Moonlight.ApiServer/Implementations/LocalOAuth2Provider.cs b/Moonlight.ApiServer/Implementations/LocalOAuth2Provider.cs index 98a12200..3586e74a 100644 --- a/Moonlight.ApiServer/Implementations/LocalOAuth2Provider.cs +++ b/Moonlight.ApiServer/Implementations/LocalOAuth2Provider.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; using MoonCore.Exceptions; using MoonCore.Extended.Abstractions; using MoonCore.Helpers; diff --git a/Moonlight.ApiServer/Implementations/Metrics/ApplicationMetric.cs b/Moonlight.ApiServer/Implementations/Metrics/ApplicationMetric.cs index 424910a9..acb2dddb 100644 --- a/Moonlight.ApiServer/Implementations/Metrics/ApplicationMetric.cs +++ b/Moonlight.ApiServer/Implementations/Metrics/ApplicationMetric.cs @@ -1,4 +1,5 @@ using System.Diagnostics.Metrics; +using Microsoft.Extensions.DependencyInjection; using Moonlight.ApiServer.Interfaces; using Moonlight.ApiServer.Services; diff --git a/Moonlight.ApiServer/Implementations/Metrics/UsersMetric.cs b/Moonlight.ApiServer/Implementations/Metrics/UsersMetric.cs index 698c4e8e..65bad6f1 100644 --- a/Moonlight.ApiServer/Implementations/Metrics/UsersMetric.cs +++ b/Moonlight.ApiServer/Implementations/Metrics/UsersMetric.cs @@ -1,5 +1,6 @@ using System.Diagnostics.Metrics; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; using MoonCore.Extended.Abstractions; using Moonlight.ApiServer.Database.Entities; using Moonlight.ApiServer.Interfaces; diff --git a/Moonlight.ApiServer/Implementations/Startup/CoreStartup.cs b/Moonlight.ApiServer/Implementations/Startup/CoreStartup.cs index f041be1b..12309cf5 100644 --- a/Moonlight.ApiServer/Implementations/Startup/CoreStartup.cs +++ b/Moonlight.ApiServer/Implementations/Startup/CoreStartup.cs @@ -1,3 +1,7 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using Moonlight.ApiServer.Configuration; using Moonlight.ApiServer.Database; diff --git a/Moonlight.ApiServer/Moonlight.ApiServer.csproj b/Moonlight.ApiServer/Moonlight.ApiServer.csproj index 9209e3af..ccec0c63 100644 --- a/Moonlight.ApiServer/Moonlight.ApiServer.csproj +++ b/Moonlight.ApiServer/Moonlight.ApiServer.csproj @@ -1,5 +1,5 @@  - + net9.0 enable @@ -12,12 +12,6 @@ - - - .dockerignore - false - - Moonlight.ApiServer 2.1.2 @@ -32,7 +26,6 @@ - diff --git a/Moonlight.ApiServer/Plugins/IPluginStartup.cs b/Moonlight.ApiServer/Plugins/IPluginStartup.cs index 92eac50a..91995389 100644 --- a/Moonlight.ApiServer/Plugins/IPluginStartup.cs +++ b/Moonlight.ApiServer/Plugins/IPluginStartup.cs @@ -1,3 +1,7 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.Hosting; + namespace Moonlight.ApiServer.Plugins; public interface IPluginStartup diff --git a/Moonlight.ApiServer/Program.cs b/Moonlight.ApiServer/Program.cs deleted file mode 100644 index f872198a..00000000 --- a/Moonlight.ApiServer/Program.cs +++ /dev/null @@ -1,5 +0,0 @@ -using Moonlight.ApiServer; - -var startup = new Startup(); - -await startup.Run(args); \ No newline at end of file diff --git a/Moonlight.ApiServer/Services/ApplicationService.cs b/Moonlight.ApiServer/Services/ApplicationService.cs index 35f117a9..37caef68 100644 --- a/Moonlight.ApiServer/Services/ApplicationService.cs +++ b/Moonlight.ApiServer/Services/ApplicationService.cs @@ -1,5 +1,7 @@ using System.Diagnostics; using System.Runtime.InteropServices; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; using MoonCore.Attributes; using MoonCore.Helpers; diff --git a/Moonlight.ApiServer/Services/DiagnoseService.cs b/Moonlight.ApiServer/Services/DiagnoseService.cs index a5844b04..51c9e04a 100644 --- a/Moonlight.ApiServer/Services/DiagnoseService.cs +++ b/Moonlight.ApiServer/Services/DiagnoseService.cs @@ -1,5 +1,6 @@ using Moonlight.ApiServer.Interfaces; using System.IO.Compression; +using Microsoft.Extensions.Logging; using MoonCore.Attributes; using MoonCore.Exceptions; using Moonlight.Shared.Http.Responses.Admin.Sys; diff --git a/Moonlight.ApiServer/Services/FrontendService.cs b/Moonlight.ApiServer/Services/FrontendService.cs index 0ae03fc5..a470ebe6 100644 --- a/Moonlight.ApiServer/Services/FrontendService.cs +++ b/Moonlight.ApiServer/Services/FrontendService.cs @@ -1,6 +1,7 @@ using System.IO.Compression; using System.Text; using System.Text.Json; +using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.FileProviders; using MoonCore.Attributes; using MoonCore.Exceptions; diff --git a/Moonlight.ApiServer/Services/MetricsBackgroundService.cs b/Moonlight.ApiServer/Services/MetricsBackgroundService.cs index b1e3ec91..619c4a18 100644 --- a/Moonlight.ApiServer/Services/MetricsBackgroundService.cs +++ b/Moonlight.ApiServer/Services/MetricsBackgroundService.cs @@ -1,4 +1,7 @@ using System.Diagnostics.Metrics; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; using Moonlight.ApiServer.Configuration; using Moonlight.ApiServer.Interfaces; diff --git a/Moonlight.ApiServer/Startup.cs b/Moonlight.ApiServer/Startup.cs index 4cd04911..f1d27512 100644 --- a/Moonlight.ApiServer/Startup.cs +++ b/Moonlight.ApiServer/Startup.cs @@ -2,8 +2,14 @@ using System.Text; using System.Text.Json; using Hangfire; using Hangfire.EntityFrameworkCore; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Cors.Infrastructure; +using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; using Microsoft.IdentityModel.Tokens; using MoonCore.EnvConfiguration; using MoonCore.Extended.Abstractions; @@ -27,12 +33,11 @@ namespace Moonlight.ApiServer; // Cry about it #pragma warning disable ASP0000 -public class Startup +public class StartupX { private string[] Args; // Logging - private ILoggerProvider[] LoggerProviders; private ILoggerFactory LoggerFactory; private ILogger Logger; diff --git a/Moonlight.ApiServer/Startup/CleanStartup.cs b/Moonlight.ApiServer/Startup/CleanStartup.cs new file mode 100644 index 00000000..037922da --- /dev/null +++ b/Moonlight.ApiServer/Startup/CleanStartup.cs @@ -0,0 +1,66 @@ +using Microsoft.AspNetCore.Builder; +using Moonlight.ApiServer.Configuration; +using Moonlight.ApiServer.Plugins; + +namespace Moonlight.ApiServer.Startup; + +public partial class CleanStartup +{ + // Logger + private ILogger Logger; + + // Configuration + private AppConfiguration Configuration; + + // WebApplication Stuff + private WebApplication WebApplication; + private WebApplicationBuilder WebApplicationBuilder; + + public async Task AddMoonlight( + WebApplicationBuilder builder, + string[] args, + IPluginStartup[]? plugins = null + ) + { + + } + + public async Task AddMoonlight( + WebApplication application, + string[] args, + IPluginStartup[]? plugins = null + ) + { + } + + #region Misc + + private Task PrintVersion() + { + // Fancy start console output... yes very fancy :> + var rainbow = new Crayon.Rainbow(0.5); + foreach (var c in "Moonlight") + { + Console.Write( + rainbow + .Next() + .Bold() + .Text(c.ToString()) + ); + } + + Console.WriteLine(); + + return Task.CompletedTask; + } + + private Task CreateStorage() + { + Directory.CreateDirectory("storage"); + Directory.CreateDirectory(Path.Combine("storage", "logs")); + + return Task.CompletedTask; + } + + #endregion +} \ No newline at end of file diff --git a/Moonlight.ApiServer/Startup/Startup.Auth.cs b/Moonlight.ApiServer/Startup/Startup.Auth.cs new file mode 100644 index 00000000..adb440e4 --- /dev/null +++ b/Moonlight.ApiServer/Startup/Startup.Auth.cs @@ -0,0 +1,6 @@ +namespace Moonlight.ApiServer.Startup; + +public partial class CleanStartup +{ + +} \ No newline at end of file diff --git a/Moonlight.ApiServer/Startup/Startup.Base.cs b/Moonlight.ApiServer/Startup/Startup.Base.cs new file mode 100644 index 00000000..3fcb9a0b --- /dev/null +++ b/Moonlight.ApiServer/Startup/Startup.Base.cs @@ -0,0 +1,74 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using MoonCore.Extended.Extensions; +using MoonCore.Extensions; +using MoonCore.Helpers; +using Moonlight.ApiServer.Plugins; + +namespace Moonlight.ApiServer.Startup; + +public partial class CleanStartup +{ + private Task RegisterBase(IPluginStartup[] pluginStartups) + { + WebApplicationBuilder.Services.AutoAddServices(); + WebApplicationBuilder.Services.AddHttpClient(); + + WebApplicationBuilder.Services.AddApiExceptionHandler(); + + // Add pre-existing services + WebApplicationBuilder.Services.AddSingleton(Configuration); + + // Configure controllers + var mvcBuilder = WebApplicationBuilder.Services.AddControllers(); + + // Add plugin assemblies as application parts + foreach (var pluginStartup in pluginStartups.Select(x => x.GetType().Assembly).Distinct()) + mvcBuilder.AddApplicationPart(pluginStartup.GetType().Assembly); + + return Task.CompletedTask; + } + + private Task UseBase() + { + WebApplication.UseRouting(); + WebApplication.UseExceptionHandler(); + + if (Configuration.Client.Enable) + { + if (WebApplication.Environment.IsDevelopment()) + WebApplication.UseWebAssemblyDebugging(); + + WebApplication.UseBlazorFrameworkFiles(); + WebApplication.UseStaticFiles(); + } + + return Task.CompletedTask; + } + + private Task MapBase() + { + WebApplication.MapControllers(); + + if (Configuration.Client.Enable) + WebApplication.MapFallbackToController("Index", "Frontend"); + + return Task.CompletedTask; + } + + private Task ConfigureKestrel() + { + WebApplicationBuilder.WebHost.ConfigureKestrel(kestrelOptions => + { + var maxUploadInBytes = ByteConverter + .FromMegaBytes(Configuration.Kestrel.UploadLimit) + .Bytes; + + kestrelOptions.Limits.MaxRequestBodySize = maxUploadInBytes; + }); + + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/Moonlight.ApiServer/Startup/Startup.Config.cs b/Moonlight.ApiServer/Startup/Startup.Config.cs new file mode 100644 index 00000000..adb440e4 --- /dev/null +++ b/Moonlight.ApiServer/Startup/Startup.Config.cs @@ -0,0 +1,6 @@ +namespace Moonlight.ApiServer.Startup; + +public partial class CleanStartup +{ + +} \ No newline at end of file diff --git a/Moonlight.ApiServer/Startup/Startup.Database.cs b/Moonlight.ApiServer/Startup/Startup.Database.cs new file mode 100644 index 00000000..adb440e4 --- /dev/null +++ b/Moonlight.ApiServer/Startup/Startup.Database.cs @@ -0,0 +1,6 @@ +namespace Moonlight.ApiServer.Startup; + +public partial class CleanStartup +{ + +} \ No newline at end of file diff --git a/Moonlight.ApiServer/Startup/Startup.Hangfire.cs b/Moonlight.ApiServer/Startup/Startup.Hangfire.cs new file mode 100644 index 00000000..adb440e4 --- /dev/null +++ b/Moonlight.ApiServer/Startup/Startup.Hangfire.cs @@ -0,0 +1,6 @@ +namespace Moonlight.ApiServer.Startup; + +public partial class CleanStartup +{ + +} \ No newline at end of file diff --git a/Moonlight.ApiServer/Startup/Startup.Logging.cs b/Moonlight.ApiServer/Startup/Startup.Logging.cs new file mode 100644 index 00000000..adb440e4 --- /dev/null +++ b/Moonlight.ApiServer/Startup/Startup.Logging.cs @@ -0,0 +1,6 @@ +namespace Moonlight.ApiServer.Startup; + +public partial class CleanStartup +{ + +} \ No newline at end of file diff --git a/Moonlight.ApiServer/Startup/Startup.Misc.cs b/Moonlight.ApiServer/Startup/Startup.Misc.cs new file mode 100644 index 00000000..adb440e4 --- /dev/null +++ b/Moonlight.ApiServer/Startup/Startup.Misc.cs @@ -0,0 +1,6 @@ +namespace Moonlight.ApiServer.Startup; + +public partial class CleanStartup +{ + +} \ No newline at end of file diff --git a/Moonlight.ApiServer/Startup/Startup.Plugins.cs b/Moonlight.ApiServer/Startup/Startup.Plugins.cs new file mode 100644 index 00000000..a2342ac8 --- /dev/null +++ b/Moonlight.ApiServer/Startup/Startup.Plugins.cs @@ -0,0 +1,89 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using MoonCore.Logging; +using Moonlight.ApiServer.Plugins; + +namespace Moonlight.ApiServer.Startup; + +public partial class CleanStartup +{ + private IServiceProvider PluginLoadServiceProvider; + private IPluginStartup[] PluginStartups; + + private Task InitializePlugins(IPluginStartup[] pluginStartups) + { + // Create service provider for starting up + var serviceCollection = new ServiceCollection(); + + serviceCollection.AddSingleton(Configuration); + + serviceCollection.AddLogging(builder => + { + builder.ClearProviders(); + builder.AddAnsiConsole(); + }); + + PluginLoadServiceProvider = serviceCollection.BuildServiceProvider(); + + PluginStartups = pluginStartups; + + return Task.CompletedTask; + } + + private async Task HookPluginBuild() + { + foreach (var pluginAppStartup in PluginStartups) + { + try + { + await pluginAppStartup.BuildApplication(PluginLoadServiceProvider, WebApplicationBuilder); + } + 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 PluginStartups) + { + try + { + await pluginAppStartup.ConfigureApplication(PluginLoadServiceProvider, WebApplication); + } + catch (Exception e) + { + Logger.LogError( + "An error occured while processing 'ConfigureApp' for '{name}': {e}", + pluginAppStartup.GetType().FullName, + e + ); + } + } + } + + private async Task HookPluginEndpoints() + { + foreach (var pluginEndpointStartup in PluginStartups) + { + try + { + await pluginEndpointStartup.ConfigureEndpoints(PluginLoadServiceProvider, WebApplication); + } + catch (Exception e) + { + Logger.LogError( + "An error occured while processing 'ConfigureEndpoints' for '{name}': {e}", + pluginEndpointStartup.GetType().FullName, + e + ); + } + } + } +} \ No newline at end of file