2020-10-17 10:49:41 +00:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
2020-10-17 07:15:46 +00:00
|
|
|
using System.Threading.Tasks;
|
2020-10-17 10:49:41 +00:00
|
|
|
using Elsa.Activities.Http.Triggers;
|
|
|
|
|
using Elsa.Services;
|
|
|
|
|
using Elsa.Triggers;
|
2020-10-17 07:15:46 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Activities.Http.Middleware
|
|
|
|
|
{
|
2020-10-17 10:49:41 +00:00
|
|
|
public class ReceiveHttpRequestMiddleware
|
2020-10-17 07:15:46 +00:00
|
|
|
{
|
|
|
|
|
private readonly RequestDelegate _next;
|
|
|
|
|
|
2020-10-17 10:49:41 +00:00
|
|
|
public ReceiveHttpRequestMiddleware(RequestDelegate next)
|
2020-10-17 07:15:46 +00:00
|
|
|
{
|
|
|
|
|
_next = next;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 11:51:27 +00:00
|
|
|
public async Task InvokeAsync(HttpContext httpContext, IWorkflowSelector workflowSelector, IWorkflowHost workflowHost)
|
2020-10-17 07:15:46 +00:00
|
|
|
{
|
2020-10-17 10:49:41 +00:00
|
|
|
var path = httpContext.Request.Path;
|
|
|
|
|
var method = httpContext.Request.Method;
|
2020-10-17 11:51:27 +00:00
|
|
|
var cancellationToken = httpContext.RequestAborted;
|
2020-10-17 10:49:41 +00:00
|
|
|
|
|
|
|
|
var results = await workflowSelector.SelectWorkflowsAsync<ReceiveHttpRequestTrigger>(
|
|
|
|
|
x => x.Path == path && x.Method == null || x.Method == method,
|
|
|
|
|
cancellationToken).ToListAsync(cancellationToken);
|
2020-10-17 07:15:46 +00:00
|
|
|
|
2020-10-17 10:49:41 +00:00
|
|
|
var result = results.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (result == null)
|
|
|
|
|
await _next(httpContext);
|
|
|
|
|
else
|
|
|
|
|
await workflowHost.RunWorkflowAsync(result.WorkflowBlueprint, result.ActivityId, cancellationToken: cancellationToken);
|
2020-10-17 07:15:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|