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() is not IApiResolutionService resolution) return; if (resolution.QueryRoutes() is not ImmutableList> 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; } }); } } }