Adds SlpModularCms.Api.Slave for local master/slave dev testing (Unit 1)
Relocates ModuleOrchestrator, ServiceCollectionExtensions, and ApiPrefixConvention from SlpModularCms.Api into SlpModularCms.Core.Hosting so a new Master-less SlpModularCms.Api.Slave host project (ports 5285/7222) can share the same bootstrap code without duplicating it. This lets a developer run a master instance and a slave instance side by side locally to test the master/slave connection, without touching the existing master/slave protocol itself. Relocates the two orchestrator/convention test files from Modules.Identity.Tests to Core.Tests, dropping an incidental ProjectReference to SlpModularCms.Api that existed only for those tests. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
using System.Reflection;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SlpModularCms.Core.Modules;
|
||||
|
||||
namespace SlpModularCms.Core.Hosting;
|
||||
|
||||
/// <summary>
|
||||
/// Orkestrator die modules ontdekt en hun lifecycle beheert.
|
||||
/// </summary>
|
||||
public class ModuleOrchestrator
|
||||
{
|
||||
private readonly List<IModule> _modules = new();
|
||||
private readonly ILogger<ModuleOrchestrator> _logger;
|
||||
|
||||
public ModuleOrchestrator(ILogger<ModuleOrchestrator> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void DiscoverModules()
|
||||
{
|
||||
_logger.LogInformation("Start module discovery...");
|
||||
|
||||
// Forceer het laden van module assemblies van disk
|
||||
var path = AppDomain.CurrentDomain.BaseDirectory;
|
||||
var moduleFiles = Directory.GetFiles(path, "SlpModularCms.Modules.*.dll");
|
||||
|
||||
foreach (var file in moduleFiles)
|
||||
{
|
||||
try
|
||||
{
|
||||
var assemblyName = AssemblyName.GetAssemblyName(file);
|
||||
if (AppDomain.CurrentDomain.GetAssemblies().All(a => a.FullName != assemblyName.FullName))
|
||||
{
|
||||
Assembly.Load(assemblyName);
|
||||
_logger.LogDebug("Assembly geladen: {AssemblyName}", assemblyName.Name);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Kon assembly niet laden van disk: {FilePath}", file);
|
||||
}
|
||||
}
|
||||
|
||||
var assemblies = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.Where(a => a.FullName != null && a.FullName.StartsWith("SlpModularCms.Modules"))
|
||||
.ToList();
|
||||
|
||||
foreach (var assembly in assemblies)
|
||||
{
|
||||
var moduleTypes = assembly.GetTypes()
|
||||
.Where(t => typeof(IModule).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract);
|
||||
|
||||
foreach (var type in moduleTypes)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Activator.CreateInstance(type) is IModule module)
|
||||
{
|
||||
_modules.Add(module);
|
||||
_logger.LogInformation("Module ontdekt: {ModuleName} v{Version}", module.Name, module.Version);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Fout bij het instantiëren van module type {TypeName}", type.FullName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("{Count} modules succesvol geladen.", _modules.Count);
|
||||
}
|
||||
|
||||
public void RegisterModuleServices(IServiceCollection services)
|
||||
{
|
||||
foreach (var module in _modules)
|
||||
{
|
||||
try
|
||||
{
|
||||
module.RegisterServices(services);
|
||||
_logger.LogInformation("Services geregistreerd voor module: {ModuleName}", module.Name);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Fout bij het registreren van services voor module {ModuleName}", module.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void UseModules(IApplicationBuilder app)
|
||||
{
|
||||
foreach (var module in _modules)
|
||||
{
|
||||
try
|
||||
{
|
||||
module.UseModule(app);
|
||||
_logger.LogInformation("Module geactiveerd in pipeline: {ModuleName}", module.Name);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Fout bij het activeren van module {ModuleName} in de pipeline", module.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user