elsa-core/src/modules/Elsa.Tenants.AspNetCore/Features/MultitenantHttpRoutingFeature.cs
Sipke Schoorstra d83bb3b834 Add customizable tenant header support for HTTP routing
Introduced `MultitenancyHttpOptions` to configure the tenant header name used for HTTP routing. Updated `HeaderTenantResolver` to utilize this option and modified the server setup to allow overriding the default header name. This enables more flexible multitenancy configurations.
2025-02-09 08:24:55 +01:00

58 lines
2.4 KiB
C#

using Elsa.Common.Multitenancy;
using Elsa.Features.Abstractions;
using Elsa.Features.Attributes;
using Elsa.Features.Services;
using Elsa.Http.Features;
using Elsa.Tenants.AspNetCore.Options;
using Elsa.Tenants.AspNetCore.Services;
using Elsa.Tenants.Features;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Tenants.AspNetCore.Features;
[DependencyOf(typeof(HttpFeature))]
[DependencyOf(typeof(TenantsFeature))]
public class MultitenantHttpRoutingFeature(IModule module) : FeatureBase(module)
{
private Action<MultitenancyHttpOptions> _configureMultitenancyHttpOptions = _ => { };
/// <summary>
/// Configures the MultitenantHttpRoutingFeature to use a specific tenant header name.
/// </summary>
/// <param name="headerName">The name of the HTTP header used to identify the tenant.</param>
/// <returns>The current instance of <see cref="MultitenantHttpRoutingFeature"/> for fluent configuration.</returns>
public MultitenantHttpRoutingFeature WithTenantHeader(string headerName) => WithMultitenancyHttpOptions(options => options.TenantHeaderName = headerName);
/// <summary>
/// Configures the MultitenantHttpRoutingFeature with custom multitenancy HTTP options.
/// </summary>
/// <param name="configure">The action to configure <see cref="MultitenancyHttpOptions"/>.</param>
/// <returns>The current instance of <see cref="MultitenantHttpRoutingFeature"/> for fluent configuration.</returns>
public MultitenantHttpRoutingFeature WithMultitenancyHttpOptions(Action<MultitenancyHttpOptions> configure)
{
_configureMultitenancyHttpOptions = configure;
return this;
}
public override void Configure()
{
Module.Configure<HttpFeature>(feature =>
{
feature.WithHttpEndpointRoutesProvider<TenantPrefixHttpEndpointRoutesProvider>();
feature.WithHttpEndpointBasePathProvider<TenantPrefixHttpEndpointBasePathProvider>();
});
}
public override void Apply()
{
// Multitenancy HTTP options.
Services.Configure(_configureMultitenancyHttpOptions);
// Tenant resolvers.
Services
.AddScoped<ITenantResolver, RoutePrefixTenantResolver>()
.AddScoped<ITenantResolver, HeaderTenantResolver>()
.AddScoped<ITenantResolver, HostTenantResolver>()
.AddScoped<TenantPrefixHttpEndpointRoutesProvider>();
}
}