Initial API setup and basic program functioning

This commit is contained in:
2026-03-24 12:29:07 +10:30
parent e5fcaad365
commit f26ff6e04a
17 changed files with 279 additions and 0 deletions
@@ -0,0 +1,39 @@
using Emergence.api.Interfaces;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Reflection;
namespace Emergence.api.Extensions;
public static class EndpointExtension
{
public static IServiceCollection AddEndpoints(this IServiceCollection services, Assembly assembly)
{
// Find all classes that implement IEndpoint and register them for DI
ServiceDescriptor[] serviceDescriptors = assembly
.DefinedTypes
.Where(type => type is { IsAbstract: false, IsInterface: false } &&
type.IsAssignableTo(typeof(IEndpoint)))
.Select(type => ServiceDescriptor.Transient(typeof(IEndpoint), type))
.ToArray();
services.TryAddEnumerable(serviceDescriptors);
return services;
}
public static IApplicationBuilder MapEndpoints(this WebApplication app, RouteGroupBuilder? routeGroupBuilder = null)
{
IEnumerable<IEndpoint> endpoints = app.Services
.GetRequiredService<IEnumerable<IEndpoint>>();
IEndpointRouteBuilder builder =
routeGroupBuilder is null ? app : routeGroupBuilder;
foreach (IEndpoint endpoint in endpoints)
{
endpoint.MapEndPoint(builder);
}
return app;
}
}