elsa-core/src/modules/Elsa.Http/Features/HttpFeature.cs

145 lines
5.5 KiB
C#
Raw Normal View History

2022-11-23 10:20:00 +00:00
using Elsa.Common.Features;
using Elsa.Extensions;
using Elsa.Features.Abstractions;
2022-11-23 10:20:00 +00:00
using Elsa.Features.Attributes;
using Elsa.Features.Services;
using Elsa.Http.ContentWriters;
using Elsa.Http.Contracts;
using Elsa.Http.Handlers;
using Elsa.Http.HostedServices;
using Elsa.Http.Models;
using Elsa.Http.Options;
using Elsa.Http.Parsers;
using Elsa.Http.PortResolvers;
2023-01-24 21:39:31 +00:00
using Elsa.Http.Providers;
using Elsa.Http.Services;
2023-02-15 11:09:45 +00:00
using Elsa.JavaScript.Features;
using Elsa.Liquid.Features;
using Elsa.Workflows.Core.Contracts;
using Elsa.Workflows.Management.Requests;
using Elsa.Workflows.Management.Responses;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Http.Features;
2023-02-15 11:09:45 +00:00
/// <summary>
/// Installs services related to HTTP services and activities.
/// </summary>
2022-11-23 10:20:00 +00:00
[DependsOn(typeof(MemoryCacheFeature))]
2023-02-15 11:09:45 +00:00
[DependsOn(typeof(LiquidFeature))]
[DependsOn(typeof(JavaScriptFeature))]
public class HttpFeature : FeatureBase
{
2023-02-15 11:09:45 +00:00
/// <inheritdoc />
public HttpFeature(IModule module) : base(module)
{
}
/// <summary>
/// A delegate to configure <see cref="HttpActivityOptions"/>.
/// </summary>
public Action<HttpActivityOptions>? ConfigureHttpOptions { get; set; }
2023-02-15 11:09:45 +00:00
/// <summary>
/// A delegate that is invoked when authorizing an inbound HTTP request.
/// </summary>
public Func<IServiceProvider, IHttpEndpointAuthorizationHandler> HttpEndpointAuthorizationHandler { get; set; } = sp => sp.GetRequiredService<AllowAnonymousHttpEndpointAuthorizationHandler>();
2023-02-15 11:09:45 +00:00
/// <summary>
/// A delegate that is invoked when an HTTP workflow faults.
/// </summary>
public Func<IServiceProvider, IHttpEndpointWorkflowFaultHandler> HttpEndpointWorkflowFaultHandler { get; set; } = sp => sp.GetRequiredService<DefaultHttpEndpointWorkflowFaultHandler>();
/// <summary>
/// A delegate to configure the <see cref="HttpClient"/> used when by the <see cref="FlowSendHttpRequest"/> activity.
/// </summary>
public Action<IServiceProvider, HttpClient> HttpClient { get; set; } = (_, _) => { };
/// <summary>
/// A delegate to configure the <see cref="HttpClientBuilder"/> for <see cref="HttpClient"/>.
/// </summary>
public Action<IHttpClientBuilder> HttpClientBuilder { get; set; } = _ => { };
/// <inheritdoc />
public override void Configure()
{
Module.UseWorkflowManagement(management =>
{
management.AddVariableTypes(new[]
{
typeof(RouteData),
typeof(HttpRequest),
typeof(HttpResponse),
typeof(HttpResponseMessage),
typeof(HttpRequestHeaders)
}, "HTTP");
management.AddActivitiesFrom<HttpFeature>();
});
Services.AddRequestHandler<ValidateWorkflowRequestHandler, ValidateWorkflowRequest, ValidateWorkflowResponse>();
}
/// <inheritdoc />
public override void ConfigureHostedServices()
{
ConfigureHostedService<UpdateRouteTableHostedService>();
}
/// <inheritdoc />
public override void Apply()
{
var configureOptions = ConfigureHttpOptions ?? (options =>
{
options.BasePath = "/workflows";
options.BaseUrl = new Uri("http://localhost");
});
Services.Configure(configureOptions);
var httpClientBuilder = Services.AddHttpClient<SendHttpRequestBase>(HttpClient);
HttpClientBuilder(httpClientBuilder);
Services
.AddSingleton<IRouteMatcher, RouteMatcher>()
.AddSingleton<IRouteTable, RouteTable>()
.AddSingleton<IAbsoluteUrlProvider, DefaultAbsoluteUrlProvider>()
2023-01-24 20:03:50 +00:00
.AddSingleton<IHttpBookmarkProcessor, HttpBookmarkProcessor>()
.AddSingleton<IRouteTableUpdater, DefaultRouteTableUpdater>()
.AddNotificationHandlersFrom<UpdateRouteTable>()
.AddHttpContextAccessor()
2023-01-24 21:39:31 +00:00
// Content parsers.
.AddSingleton<IHttpContentParser, StringHttpContentParser>()
.AddSingleton<IHttpContentParser, JsonHttpContentParser>()
.AddSingleton<IHttpContentParser, XmlHttpContentParser>()
2023-01-24 21:39:31 +00:00
// HTTP content factories.
2023-01-24 20:48:05 +00:00
.AddSingleton<IHttpContentFactory, TextContentFactory>()
.AddSingleton<IHttpContentFactory, JsonContentFactory>()
2023-01-24 21:39:31 +00:00
.AddSingleton<IHttpContentFactory, XmlContentFactory>()
2023-01-24 20:48:05 +00:00
.AddSingleton<IHttpContentFactory, FormUrlEncodedHttpContentFactory>()
2023-02-15 11:09:45 +00:00
// Activity property options providers.
2023-01-24 21:39:31 +00:00
.AddSingleton<IActivityPropertyOptionsProvider, WriteHttpResponseContentTypeOptionsProvider>()
// Port resolvers.
.AddSingleton<IActivityPortResolver, SendHttpRequestActivityPortResolver>()
// Add HTTP endpoint handlers.
.AddSingleton<AuthenticationBasedHttpEndpointAuthorizationHandler>()
.AddSingleton<AllowAnonymousHttpEndpointAuthorizationHandler>()
.AddSingleton<DefaultHttpEndpointWorkflowFaultHandler>()
.AddSingleton(HttpEndpointWorkflowFaultHandler)
.AddSingleton(HttpEndpointAuthorizationHandler)
// Add mediator handlers.
.AddNotificationHandlersFrom<HttpFeature>()
// AuthenticationBasedHttpEndpointAuthorizationHandler requires Authorization services.
// We could consider creating a separate module for installing authorization services.
.AddAuthorization();
}
}