using Elsa.Common.Features;
using Elsa.Extensions;
using Elsa.Features.Abstractions;
using Elsa.Features.Attributes;
using Elsa.Features.Services;
using Elsa.Http.ContentWriters;
using Elsa.Http.Handlers;
using Elsa.Http.Implementations;
using Elsa.Http.Options;
using Elsa.Http.Parsers;
using Elsa.Http.Providers;
using Elsa.Http.Services;
using Elsa.JavaScript.Features;
using Elsa.Liquid.Features;
using Elsa.Workflows.Management.Implementations;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Http.Features;
///
/// Installs services related to HTTP services and activities.
///
[DependsOn(typeof(MemoryCacheFeature))]
[DependsOn(typeof(LiquidFeature))]
[DependsOn(typeof(JavaScriptFeature))]
public class HttpFeature : FeatureBase
{
///
public HttpFeature(IModule module) : base(module)
{
}
///
/// A delegate to configure .
///
public Action? ConfigureHttpOptions { get; set; }
///
/// A delegate that is invoked when authorizing an inbound HTTP request.
///
public Func HttpEndpointAuthorizationHandler { get; set; } = ActivatorUtilities.GetServiceOrCreateInstance;
///
/// A delegate that is invoked when an HTTP workflow faults.
///
public Func HttpEndpointWorkflowFaultHandler { get; set; } = ActivatorUtilities.GetServiceOrCreateInstance;
///
/// A delegate to configure the used when by the activity.
///
public Action HttpClient { get; set; } = (_, _) => { };
///
/// A delegate to configure the for .
///
public Action HttpClientBuilder { get; set; } = _ => { };
///
public override void Configure()
{
Module.UseWorkflowManagement(management =>
{
management.AddVariableTypes(new[]
{
typeof(RouteData),
typeof(HttpRequest),
typeof(HttpResponse)
}, "HTTP");
management.AddActivitiesFrom();
});
}
///
public override void Apply()
{
var configureOptions = ConfigureHttpOptions ?? (options =>
{
options.BasePath = "/workflows";
options.BaseUrl = new Uri("http://localhost");
});
Services.Configure(configureOptions);
var httpClientBuilder = Services.AddHttpClient(HttpClient);
HttpClientBuilder(httpClientBuilder);
Services
.AddSingleton()
.AddSingleton()
.AddSingleton()
.AddSingleton()
.AddNotificationHandlersFrom()
.AddHttpContextAccessor()
// Content parsers.
.AddSingleton()
.AddSingleton()
.AddSingleton()
// HTTP content factories.
.AddSingleton()
.AddSingleton()
.AddSingleton()
.AddSingleton()
// Activity property options providers.
.AddSingleton()
;
}
}