You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Framework/Connected.Rest/Middleware/RestStartup.cs

50 lines
1.1 KiB

2 years ago
using System.Collections.Immutable;
using Connected.Annotations;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
[assembly: MicroService(MicroServiceType.Runtime)]
namespace Connected.Rest;
internal class RestStartup : Startup
{
protected override void OnConfigureServices(IServiceCollection services)
{
services.AddSingleton(typeof(IApiResolutionService), typeof(ApiResolutionService));
}
protected override void OnConfigure(WebApplication app)
{
RegisterRoutes(app);
}
private void RegisterRoutes(WebApplication app)
{
if (app.Services.GetService<IApiResolutionService>() is not IApiResolutionService resolution)
return;
if (resolution.QueryRoutes() is not ImmutableList<Tuple<string, ServiceMethodVerbs>> routes || !routes.Any())
return;
foreach (var route in routes)
{
app.Map(route.Item1, async (httpContext) =>
{
try
{
using var handler = new ApiServiceRequestDelegate(httpContext);
await handler.InvokeAsync();
}
catch
{
//TODO: log exception
throw;
}
});
}
}
}