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

218 lines
8.7 KiB
C#
Raw Normal View History

2022-11-23 10:20:00 +00:00
using Elsa.Common.Features;
using Elsa.Expressions.Options;
using Elsa.Extensions;
using Elsa.Features.Abstractions;
2022-11-23 10:20:00 +00:00
using Elsa.Features.Attributes;
using Elsa.Features.Services;
2023-10-12 12:20:15 +00:00
using Elsa.Http.ActivityOptionProviders;
using Elsa.Http.ContentWriters;
using Elsa.Http.Contracts;
using Elsa.Http.DownloadableContentHandlers;
using Elsa.Http.FileCaches;
using Elsa.Http.Handlers;
using Elsa.Http.HostedServices;
using Elsa.Http.Models;
using Elsa.Http.Options;
using Elsa.Http.Parsers;
using Elsa.Http.PortResolvers;
using Elsa.Http.Selectors;
using Elsa.Http.Services;
using Elsa.Workflows.Core.Contracts;
using Elsa.Workflows.Management.Requests;
using Elsa.Workflows.Management.Responses;
using FluentStorage;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
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))]
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; }
/// <summary>
/// A delegate to configure <see cref="HttpFileCacheOptions"/>.
/// </summary>
public Action<HttpFileCacheOptions>? ConfigureHttpFileCacheOptions { 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, IHttpEndpointFaultHandler> HttpEndpointWorkflowFaultHandler { get; set; } = sp => sp.GetRequiredService<DefaultHttpEndpointFaultHandler>();
/// <summary>
/// A delegate to configure the <see cref="IContentTypeProvider"/>.
/// </summary>
public Func<IServiceProvider, IContentTypeProvider> ContentTypeProvider { get; set; } = _ => new FileExtensionContentTypeProvider();
/// <summary>
/// A delegate to configure the <see cref="IFileCacheStorageProvider"/>.
/// </summary>
public Func<IServiceProvider, IFileCacheStorageProvider> FileCache { get; set; } = sp =>
{
var options = sp.GetRequiredService<IOptions<HttpFileCacheOptions>>().Value;
var blobStorage = StorageFactory.Blobs.DirectoryFiles(options.LocalCacheDirectory);
return new BlobFileCacheStorageProvider(blobStorage);
};
/// <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; } = _ => { };
/// <summary>
/// A list of <see cref="IHttpCorrelationIdSelector"/> types to register with the service collection.
/// </summary>
public ICollection<Type> HttpCorrelationIdSelectorTypes { get; } = new List<Type>
{
typeof(HeaderHttpCorrelationIdSelector),
typeof(QueryStringHttpCorrelationIdSelector)
};
/// <summary>
/// A list of <see cref="IHttpWorkflowInstanceIdSelector"/> types to register with the service collection.
/// </summary>
public ICollection<Type> HttpWorkflowInstanceIdSelectorTypes { get; } = new List<Type>
{
typeof(HeaderHttpWorkflowInstanceIdSelector),
typeof(QueryStringHttpWorkflowInstanceIdSelector)
};
/// <inheritdoc />
public override void Configure()
{
Module.UseWorkflowManagement(management =>
{
management.AddVariableTypes(new[]
{
typeof(RouteData),
typeof(HttpRequest),
typeof(HttpResponse),
typeof(HttpResponseMessage),
typeof(HttpRequestHeaders),
typeof(IFormFile)
}, "HTTP");
management.AddActivitiesFrom<HttpFeature>();
});
}
/// <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");
});
var configureFileCacheOptions = ConfigureHttpFileCacheOptions ?? (options => { options.TimeToLive = TimeSpan.FromDays(7); });
Services.Configure(configureOptions);
Services.Configure(configureFileCacheOptions);
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>()
.AddSingleton(ContentTypeProvider)
.AddHttpContextAccessor()
// Handlers.
.AddRequestHandler<ValidateWorkflowRequestHandler, ValidateWorkflowRequest, ValidateWorkflowResponse>()
.AddNotificationHandler<UpdateRouteTable>()
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-10-12 12:20:15 +00:00
.AddSingleton<IActivityPropertyOptionsProvider, HttpContentTypeOptionsProvider>()
// Port resolvers.
.AddSingleton<IActivityResolver, SendHttpRequestActivityResolver>()
// HTTP endpoint handlers.
.AddSingleton<AuthenticationBasedHttpEndpointAuthorizationHandler>()
.AddSingleton<AllowAnonymousHttpEndpointAuthorizationHandler>()
.AddSingleton<DefaultHttpEndpointFaultHandler>()
.AddSingleton(HttpEndpointWorkflowFaultHandler)
.AddSingleton(HttpEndpointAuthorizationHandler)
// Downloadable content handlers.
.AddSingleton<IDownloadableManager, DefaultDownloadableManager>()
.AddSingleton<IDownloadableContentHandler, MultiDownloadableContentHandler>()
.AddSingleton<IDownloadableContentHandler, BinaryDownloadableContentHandler>()
.AddSingleton<IDownloadableContentHandler, StreamDownloadableContentHandler>()
.AddSingleton<IDownloadableContentHandler, FormFileDownloadableContentHandler>()
.AddSingleton<IDownloadableContentHandler, DownloadableDownloadableContentHandler>()
.AddSingleton<IDownloadableContentHandler, UrlDownloadableContentHandler>()
// File caches.
.AddSingleton(FileCache)
.AddSingleton<ZipManager>()
// AuthenticationBasedHttpEndpointAuthorizationHandler requires Authorization services.
// We could consider creating a separate module for installing authorization services.
.AddAuthorization();
// HTTP clients.
Services.AddHttpClient<IFileDownloader, HttpClientFileDownloader>();
// Add selectors.
foreach (var httpCorrelationIdSelectorType in HttpCorrelationIdSelectorTypes)
Services.AddSingleton(typeof(IHttpCorrelationIdSelector), httpCorrelationIdSelectorType);
foreach (var httpWorkflowInstanceIdSelectorType in HttpWorkflowInstanceIdSelectorTypes)
Services.AddSingleton(typeof(IHttpWorkflowInstanceIdSelector), httpWorkflowInstanceIdSelectorType);
Services.Configure<ExpressionOptions>(options =>
{
options.AddTypeAlias<IFormFile>("FormFile");
options.AddTypeAlias<IFormFile[]>("FormFile[]");
});
}
}