| | | 1 | | using Elsa.Common.Multitenancy; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | using Elsa.Http; |
| | | 4 | | using Elsa.Http.Contexts; |
| | | 5 | | using Elsa.Http.Options; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using Microsoft.AspNetCore.Routing; |
| | | 8 | | using Microsoft.Extensions.Options; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Tenants.AspNetCore.Services; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides a route for an HTTP endpoint based on the specified context and tenant prefix. |
| | | 14 | | /// </summary> |
| | | 15 | | [UsedImplicitly] |
| | 0 | 16 | | public class TenantPrefixHttpEndpointRoutesProvider(ITenantsProvider tenantsProvider, IOptions<HttpActivityOptions> opti |
| | | 17 | | { |
| | | 18 | | public async Task<IEnumerable<HttpRouteData>> GetRoutesAsync(HttpEndpointRouteProviderContext context) |
| | | 19 | | { |
| | 0 | 20 | | var routes = new List<HttpRouteData>(); |
| | 0 | 21 | | var path = context.Payload.Path; |
| | | 22 | | |
| | 0 | 23 | | if (string.IsNullOrWhiteSpace(path)) |
| | 0 | 24 | | return routes; |
| | | 25 | | |
| | 0 | 26 | | await AddTenantRouteAsync(context, routes); |
| | 0 | 27 | | AddDefaultRoute(context, routes); |
| | 0 | 28 | | return routes; |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | private async Task AddTenantRouteAsync(HttpEndpointRouteProviderContext context, List<HttpRouteData> routes) |
| | | 32 | | { |
| | 0 | 33 | | var segments = new List<string?>(); |
| | 0 | 34 | | var routeDataValues = new RouteValueDictionary(); |
| | | 35 | | |
| | 0 | 36 | | if (!string.IsNullOrEmpty(context.TenantId)) |
| | | 37 | | { |
| | 0 | 38 | | var tenant = await tenantsProvider.FindByIdAsync(context.TenantId); |
| | 0 | 39 | | var tenantPrefix = tenant?.GetRoutePrefix(); |
| | | 40 | | |
| | 0 | 41 | | if (!string.IsNullOrWhiteSpace(tenantPrefix)) |
| | | 42 | | { |
| | 0 | 43 | | segments.Add("{tenantPrefix}"); |
| | 0 | 44 | | routeDataValues.Add("tenantId", tenant!.Id); |
| | | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | var path = context.Payload.Path; |
| | 0 | 49 | | segments.AddRange(options.Value.BasePath.ToString(), path); |
| | 0 | 50 | | var routeData = new HttpRouteData(segments.JoinSegments(), routeDataValues); |
| | 0 | 51 | | routes.Add(routeData); |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | private void AddDefaultRoute(HttpEndpointRouteProviderContext context, List<HttpRouteData> routes) |
| | | 55 | | { |
| | 0 | 56 | | var basePath = options.Value.BasePath.ToString(); |
| | 0 | 57 | | var path = context.Payload.Path; |
| | 0 | 58 | | var routeData = new HttpRouteData(new[] |
| | 0 | 59 | | { |
| | 0 | 60 | | basePath, path |
| | 0 | 61 | | }.JoinSegments()); |
| | 0 | 62 | | routes.Add(routeData); |
| | 0 | 63 | | } |
| | | 64 | | } |