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

116 lines
4.2 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.Options;
using Elsa.Http.Parsers;
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.Management.Contracts;
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>
2023-01-24 20:03:50 +00:00
public Func<IServiceProvider, IHttpEndpointAuthorizationHandler> HttpEndpointAuthorizationHandler { get; set; } = ActivatorUtilities.GetServiceOrCreateInstance<AllowAnonymousHttpEndpointAuthorizationHandler>;
2023-02-15 11:09:45 +00:00
/// <summary>
/// A delegate that is invoked when an HTTP workflow faults.
/// </summary>
2023-01-24 20:03:50 +00:00
public Func<IServiceProvider, IHttpEndpointWorkflowFaultHandler> HttpEndpointWorkflowFaultHandler { get; set; } = ActivatorUtilities.GetServiceOrCreateInstance<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)
}, "HTTP");
management.AddActivitiesFrom<HttpFeature>();
});
}
/// <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>()
.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>()
// Add Http endpoint handlers
.AddSingleton(HttpEndpointWorkflowFaultHandler)
;
}
}