2022-07-21 14:18:42 +00:00
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
using Elsa.ActivityDefinitions.EntityFrameworkCore.Extensions;
|
|
|
|
|
using Elsa.ActivityDefinitions.EntityFrameworkCore.Sqlite;
|
|
|
|
|
using Elsa.Api.Common;
|
|
|
|
|
using Elsa.Api.Common.Features;
|
|
|
|
|
using Elsa.Api.Common.Options;
|
2022-05-26 15:05:36 +00:00
|
|
|
using Elsa.AspNetCore.Extensions;
|
2022-05-26 10:47:31 +00:00
|
|
|
using Elsa.Extensions;
|
2022-07-21 14:18:42 +00:00
|
|
|
using Elsa.Features.Extensions;
|
2022-05-26 10:47:31 +00:00
|
|
|
using Elsa.Hangfire.Implementations;
|
|
|
|
|
using Elsa.Http;
|
|
|
|
|
using Elsa.Http.Extensions;
|
|
|
|
|
using Elsa.JavaScript.Activities;
|
|
|
|
|
using Elsa.JavaScript.Extensions;
|
|
|
|
|
using Elsa.Jobs.Extensions;
|
|
|
|
|
using Elsa.Labels.EntityFrameworkCore.Extensions;
|
|
|
|
|
using Elsa.Labels.EntityFrameworkCore.Sqlite;
|
|
|
|
|
using Elsa.Labels.Extensions;
|
|
|
|
|
using Elsa.Liquid.Extensions;
|
|
|
|
|
using Elsa.Quartz.Implementations;
|
|
|
|
|
using Elsa.Scheduling.Extensions;
|
|
|
|
|
using Elsa.WorkflowContexts.Extensions;
|
|
|
|
|
using Elsa.Workflows.Api.Extensions;
|
|
|
|
|
using Elsa.Workflows.Core.Activities;
|
2022-06-11 18:04:16 +00:00
|
|
|
using Elsa.Workflows.Core.Activities.Flowchart.Activities;
|
2022-05-26 10:47:31 +00:00
|
|
|
using Elsa.Workflows.Core.Pipelines.WorkflowExecution.Components;
|
|
|
|
|
using Elsa.Workflows.Core.Serialization;
|
|
|
|
|
using Elsa.Workflows.Management.Extensions;
|
|
|
|
|
using Elsa.Workflows.Persistence.EntityFrameworkCore.Extensions;
|
|
|
|
|
using Elsa.Workflows.Persistence.EntityFrameworkCore.Sqlite;
|
|
|
|
|
using Elsa.Workflows.Persistence.Extensions;
|
|
|
|
|
using Elsa.Workflows.Runtime.Extensions;
|
2022-07-21 14:18:42 +00:00
|
|
|
using Elsa.WorkflowServer.Web.Implementations;
|
|
|
|
|
using FastEndpoints;
|
|
|
|
|
using FastEndpoints.Security;
|
2022-05-26 10:47:31 +00:00
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
var services = builder.Services;
|
2022-07-21 14:18:42 +00:00
|
|
|
var configuration = builder.Configuration;
|
|
|
|
|
var accessTokenOptions = new AccessTokenOptions();
|
|
|
|
|
configuration.GetSection("AccessTokens").Bind(accessTokenOptions);
|
|
|
|
|
|
|
|
|
|
// Global control over Elsa API endpoints security.
|
|
|
|
|
ApiSecurityOptions.AllowAnonymous = !builder.Environment.IsProduction();
|
|
|
|
|
ApiSecurityOptions.ValidateRoles = builder.Environment.IsProduction();
|
|
|
|
|
|
|
|
|
|
// Add application-specific services.
|
|
|
|
|
services.AddSingleton<CustomCredentialsValidator>();
|
|
|
|
|
services.AddSingleton<CustomAccessTokenIssuer>();
|
2022-05-26 10:47:31 +00:00
|
|
|
|
|
|
|
|
// Add Elsa services.
|
|
|
|
|
services
|
2022-05-26 14:07:01 +00:00
|
|
|
.AddElsa(elsa => elsa
|
|
|
|
|
.UseManagement(management => management
|
|
|
|
|
.AddActivity<WriteLine>()
|
|
|
|
|
.AddActivity<ReadLine>()
|
|
|
|
|
.AddActivity<If>()
|
|
|
|
|
.AddActivity<HttpEndpoint>()
|
2022-07-26 14:01:10 +00:00
|
|
|
.AddActivity<WriteHttpResponse>()
|
2022-05-26 14:07:01 +00:00
|
|
|
.AddActivity<Flowchart>()
|
2022-06-29 10:57:01 +00:00
|
|
|
.AddActivity<FlowDecision>()
|
2022-07-07 20:08:02 +00:00
|
|
|
.AddActivity<FlowSwitch>()
|
2022-05-26 14:07:01 +00:00
|
|
|
.AddActivity<Elsa.Scheduling.Activities.Delay>()
|
|
|
|
|
.AddActivity<Elsa.Scheduling.Activities.Timer>()
|
|
|
|
|
.AddActivity<ForEach>()
|
|
|
|
|
.AddActivity<Switch>()
|
|
|
|
|
.AddActivity<RunJavaScript>()
|
2022-06-09 16:48:43 +00:00
|
|
|
.AddActivity<Event>()
|
2022-05-26 14:07:01 +00:00
|
|
|
)
|
2022-07-21 14:18:42 +00:00
|
|
|
.Use<CommonApiFeature>(feature =>
|
|
|
|
|
{
|
|
|
|
|
feature.TokenSigningKey = accessTokenOptions.SigningKey;
|
|
|
|
|
feature.CredentialsValidator = sp => sp.GetRequiredService<CustomCredentialsValidator>();
|
|
|
|
|
feature.AccessTokenIssuer = sp => sp.GetRequiredService<CustomAccessTokenIssuer>();
|
|
|
|
|
})
|
2022-05-27 10:36:39 +00:00
|
|
|
.UseWorkflowPersistence(p => p.UseEntityFrameworkCore(ef => ef.UseSqlite()))
|
2022-06-05 20:49:47 +00:00
|
|
|
.UseWorkflowApiEndpoints()
|
2022-05-26 15:05:36 +00:00
|
|
|
.UseJavaScript()
|
|
|
|
|
.UseLiquid()
|
2022-05-26 14:07:01 +00:00
|
|
|
.UseLabels(labels => labels.UseEntityFrameworkCore(ef => ef.UseSqlite()))
|
2022-07-21 14:18:42 +00:00
|
|
|
.UseCustomActivities(feature => feature.UseEntityFrameworkCore(ef => ef.UseSqlite()))
|
2022-05-26 14:07:01 +00:00
|
|
|
.UseHttp()
|
2022-05-26 15:05:36 +00:00
|
|
|
.UseMvc()
|
2022-05-26 14:07:01 +00:00
|
|
|
);
|
2022-05-26 10:47:31 +00:00
|
|
|
|
|
|
|
|
services
|
|
|
|
|
.AddJobServices(new QuartzJobSchedulerProvider(), new HangfireJobQueueProvider())
|
|
|
|
|
.AddSchedulingServices()
|
|
|
|
|
;
|
|
|
|
|
|
2022-07-21 14:18:42 +00:00
|
|
|
services.AddFastEndpoints();
|
|
|
|
|
services.AddAuthenticationJWTBearer(accessTokenOptions.SigningKey);
|
2022-05-26 10:47:31 +00:00
|
|
|
services.AddHealthChecks();
|
|
|
|
|
services.AddCors(cors => cors.AddDefaultPolicy(policy => policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()));
|
|
|
|
|
|
2022-07-21 14:18:42 +00:00
|
|
|
// Authorization policies.
|
|
|
|
|
services.AddAuthorization(options => options.AddPolicy("WorkflowManagerPolicy", policy => policy.RequireAuthenticatedUser()));
|
2022-05-26 10:47:31 +00:00
|
|
|
|
|
|
|
|
// Configure middleware pipeline.
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
var serviceProvider = app.Services;
|
|
|
|
|
|
|
|
|
|
// Configure workflow engine execution pipeline.
|
|
|
|
|
serviceProvider.ConfigureDefaultWorkflowExecutionPipeline(pipeline =>
|
|
|
|
|
pipeline
|
|
|
|
|
.UseWorkflowExecutionEvents()
|
|
|
|
|
.UseWorkflowExecutionLogPersistence()
|
|
|
|
|
.UsePersistence()
|
|
|
|
|
.UseWorkflowContexts()
|
|
|
|
|
.UseStackBasedActivityScheduler()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
|
|
|
|
|
// CORS.
|
|
|
|
|
app.UseCors();
|
|
|
|
|
|
|
|
|
|
// Health checks.
|
|
|
|
|
app.MapHealthChecks("/");
|
|
|
|
|
|
2022-07-21 14:18:42 +00:00
|
|
|
app.UseAuthentication();
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
2022-05-26 10:47:31 +00:00
|
|
|
// Map Elsa API endpoint controllers.
|
2022-07-21 14:18:42 +00:00
|
|
|
|
|
|
|
|
// Deprecated.
|
2022-05-26 14:07:01 +00:00
|
|
|
app.MapManagementApiEndpoints();
|
2022-05-26 10:47:31 +00:00
|
|
|
app.MapLabelApiEndpoints();
|
|
|
|
|
|
2022-07-21 14:18:42 +00:00
|
|
|
// Use FastEndpoints middleware.
|
|
|
|
|
|
|
|
|
|
ValueTask<object?> DeserializeRequestAsync(HttpRequest httpRequest, Type modelType, JsonSerializerContext? serializerContext, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var serializerOptionsProvider = httpRequest.HttpContext.RequestServices.GetRequiredService<SerializerOptionsProvider>();
|
|
|
|
|
var options = serializerOptionsProvider.CreateApiOptions();
|
|
|
|
|
|
|
|
|
|
return serializerContext == null
|
|
|
|
|
? JsonSerializer.DeserializeAsync(httpRequest.Body, modelType, options, cancellationToken)
|
|
|
|
|
: JsonSerializer.DeserializeAsync(httpRequest.Body, modelType, serializerContext, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Task SerializeRequestAsync(HttpResponse httpResponse, object dto, string contentType, JsonSerializerContext? serializerContext, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var serializerOptionsProvider = httpResponse.HttpContext.RequestServices.GetRequiredService<SerializerOptionsProvider>();
|
|
|
|
|
var options = serializerOptionsProvider.CreateApiOptions();
|
|
|
|
|
|
|
|
|
|
httpResponse.ContentType = contentType;
|
|
|
|
|
return serializerContext == null
|
|
|
|
|
? JsonSerializer.SerializeAsync(httpResponse.Body, dto, dto.GetType(), options, cancellationToken)
|
|
|
|
|
: JsonSerializer.SerializeAsync(httpResponse.Body, dto, dto.GetType(), serializerContext, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.UseFastEndpoints(config =>
|
|
|
|
|
{
|
|
|
|
|
config.RoutingOptions = routing => routing.Prefix = "elsa/api";
|
|
|
|
|
config.RequestDeserializer = DeserializeRequestAsync;
|
|
|
|
|
config.ResponseSerializer = SerializeRequestAsync;
|
|
|
|
|
});
|
|
|
|
|
|
2022-05-26 10:47:31 +00:00
|
|
|
// Register Elsa middleware.
|
|
|
|
|
app.UseJsonSerializationErrorHandler();
|
|
|
|
|
app.UseHttpActivities();
|
|
|
|
|
|
|
|
|
|
// Run.
|
|
|
|
|
app.Run();
|