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-05-16 17:20:59 +00:00
|
|
|
using Elsa.Services;
|
2020-10-17 07:15:46 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2021-05-16 17:20:59 +00:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Open.Linq.AsyncExtensions;
|
2020-10-17 07:15:46 +00:00
|
|
|
|
|
|
|
|
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-05-03 10:55:22 +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-05-16 17:20:59 +00:00
|
|
|
public async Task InvokeAsync(HttpContext httpContext, IWorkflowLaunchpad workflowLaunchpad)
|
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);
|
2021-05-15 20:17:25 +00:00
|
|
|
var useDispatch = httpContext.Request.GetUseDispatch();
|
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-05-16 17:20:59 +00:00
|
|
|
var collectWorkflowsContext = new CollectWorkflowsContext(activityType, bookmark, trigger, default, correlationId, default, TenantId);
|
|
|
|
|
var pendingWorkflows = await workflowLaunchpad.CollectWorkflowsAsync(collectWorkflowsContext, cancellationToken).ToList();
|
2021-01-29 15:16:45 +00:00
|
|
|
|
2021-05-15 20:17:25 +00:00
|
|
|
if (useDispatch)
|
|
|
|
|
{
|
2021-05-16 17:20:59 +00:00
|
|
|
await workflowLaunchpad.DispatchPendingWorkflowsAsync(pendingWorkflows, cancellationToken: cancellationToken);
|
|
|
|
|
|
|
|
|
|
if (pendingWorkflows.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
httpContext.Response.ContentType = "application/json";
|
|
|
|
|
await httpContext.Response.WriteAsync(JsonConvert.SerializeObject(pendingWorkflows), cancellationToken);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-05-15 20:17:25 +00:00
|
|
|
}
|
2021-05-16 17:20:59 +00:00
|
|
|
else
|
2020-10-30 20:55:27 +00:00
|
|
|
{
|
2021-05-16 17:20:59 +00:00
|
|
|
await workflowLaunchpad.ExecutePendingWorkflowsAsync(pendingWorkflows, cancellationToken: cancellationToken);
|
|
|
|
|
|
|
|
|
|
if (pendingWorkflows.Count > 0)
|
|
|
|
|
return;
|
2021-01-30 11:47:50 +00:00
|
|
|
}
|
2021-05-16 17:20:59 +00:00
|
|
|
|
|
|
|
|
await _next(httpContext);
|
2021-01-29 15:16:45 +00:00
|
|
|
}
|
2020-10-17 07:15:46 +00:00
|
|
|
}
|
|
|
|
|
}
|