using Elsa.Http.Middleware; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; // ReSharper disable once CheckNamespace namespace Elsa.Extensions; /// /// Adds extension methods to related to workflow middleware components. /// public static class ApplicationBuilderExtensions { /// /// Installs the component. /// public static IApplicationBuilder UseWorkflows(this IApplicationBuilder app) => app.UseMiddleware(); /// /// Applies an ASP.NET Core rate limiting policy to inbound HTTP workflow trigger routes. /// /// The application builder. /// The HTTP workflow trigger base path to protect. /// The registered ASP.NET Core rate limiting policy name. Leave empty to skip rate limiting. public static IApplicationBuilder UseWorkflowsRateLimiting(this IApplicationBuilder app, PathString? basePath, string? policyName) { var normalizedBasePath = basePath?.ToString(); return app.UseWorkflowsRateLimiting(normalizedBasePath, policyName); } /// /// Applies an ASP.NET Core rate limiting policy to inbound HTTP workflow trigger routes. /// /// The application builder. /// The HTTP workflow trigger base path to protect. /// The registered ASP.NET Core rate limiting policy name. Leave empty to skip rate limiting. public static IApplicationBuilder UseWorkflowsRateLimiting(this IApplicationBuilder app, string? basePath, string? policyName) { if (string.IsNullOrWhiteSpace(policyName) || string.IsNullOrWhiteSpace(basePath)) return app; var pathPrefix = NormalizeBasePath(basePath); return pathPrefix.HasValue ? app.UseRateLimitingPolicyForPath(pathPrefix, policyName, "Elsa HTTP workflow trigger rate limiting endpoint", requireMatchedEndpoint: false) : app; } private static PathString NormalizeBasePath(string basePath) { var value = basePath.Trim(); if (string.IsNullOrEmpty(value)) return PathString.Empty; value = value.Trim('/'); return string.IsNullOrEmpty(value) ? PathString.Empty : new PathString("/" + value); } }