2020-10-17 07:15:46 +00:00
|
|
|
using System.Threading.Tasks;
|
2021-01-29 15:16:45 +00:00
|
|
|
using Elsa.Activities.Http.Bookmarks;
|
2020-10-30 20:55:27 +00:00
|
|
|
using Elsa.Activities.Http.Extensions;
|
2021-04-06 19:42:06 +00:00
|
|
|
using Elsa.Dispatch;
|
|
|
|
|
using MediatR;
|
2020-10-17 07:15:46 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Activities.Http.Middleware
|
|
|
|
|
{
|
2021-03-04 10:47:20 +00:00
|
|
|
public class HttpEndpointMiddleware
|
2020-10-17 07:15:46 +00:00
|
|
|
{
|
2021-03-30 07:58:00 +00:00
|
|
|
// TODO: Design multi-tenancy.
|
2021-01-29 15:16:45 +00:00
|
|
|
private const string TenantId = default;
|
2020-10-17 07:15:46 +00:00
|
|
|
private readonly RequestDelegate _next;
|
|
|
|
|
|
2021-03-31 12:18:37 +00:00
|
|
|
public HttpEndpointMiddleware(RequestDelegate next) => _next = next;
|
2020-10-17 07:15:46 +00:00
|
|
|
|
2021-04-06 19:42:06 +00:00
|
|
|
public async Task InvokeAsync(HttpContext httpContext, IMediator mediator)
|
2020-10-17 07:15:46 +00:00
|
|
|
{
|
2021-03-04 10:47:20 +00:00
|
|
|
var path = httpContext.Request.Path.Value.ToLowerInvariant();
|
|
|
|
|
var method = httpContext.Request.Method!.ToLowerInvariant();
|
2020-10-17 11:51:27 +00:00
|
|
|
var cancellationToken = httpContext.RequestAborted;
|
2020-10-30 20:55:27 +00:00
|
|
|
httpContext.Request.TryGetCorrelationId(out var correlationId);
|
2020-10-17 13:38:27 +00:00
|
|
|
|
2021-03-31 12:18:37 +00:00
|
|
|
const string activityType = nameof(HttpEndpoint);
|
|
|
|
|
var trigger = new HttpEndpointBookmark(path, method, null);
|
|
|
|
|
var bookmark = new HttpEndpointBookmark(path, method, correlationId?.ToLowerInvariant());
|
2021-04-06 19:42:06 +00:00
|
|
|
var workflowInstanceCount = await mediator.Send(new TriggerWorkflowsRequest(activityType, bookmark, trigger, default, correlationId, default, TenantId), cancellationToken);
|
2021-01-29 15:16:45 +00:00
|
|
|
|
2021-04-06 19:42:06 +00:00
|
|
|
if (workflowInstanceCount == 0)
|
2020-10-30 20:55:27 +00:00
|
|
|
{
|
2020-10-17 10:49:41 +00:00
|
|
|
await _next(httpContext);
|
2021-01-30 11:47:50 +00:00
|
|
|
}
|
2021-01-29 15:16:45 +00:00
|
|
|
}
|
2020-10-17 07:15:46 +00:00
|
|
|
}
|
|
|
|
|
}
|