| | | 1 | | using System.Text.Encodings.Web; |
| | | 2 | | using Elsa.Caching.Options; |
| | | 3 | | using Elsa.Common.RecurringTasks; |
| | | 4 | | using Elsa.Expressions.Helpers; |
| | | 5 | | using Elsa.Extensions; |
| | | 6 | | using Elsa.Features.Services; |
| | | 7 | | using Elsa.Persistence.EFCore.Extensions; |
| | | 8 | | using Elsa.Persistence.EFCore.Modules.Management; |
| | | 9 | | using Elsa.Persistence.EFCore.Modules.Runtime; |
| | | 10 | | using Elsa.Server.Web.Activities; |
| | | 11 | | using Elsa.Server.Web.ActivityHosts; |
| | | 12 | | using Elsa.Server.Web.Filters; |
| | | 13 | | using Elsa.Tenants.AspNetCore; |
| | | 14 | | using Elsa.Tenants.Extensions; |
| | | 15 | | using Elsa.WorkflowProviders.BlobStorage.ElsaScript.Extensions; |
| | | 16 | | using Elsa.Workflows; |
| | | 17 | | using Elsa.Workflows.Activities.Flowchart.Extensions; |
| | | 18 | | using Elsa.Workflows.Api; |
| | | 19 | | using Elsa.Workflows.CommitStates.Strategies; |
| | | 20 | | using Elsa.Workflows.IncidentStrategies; |
| | | 21 | | using Elsa.Workflows.LogPersistence; |
| | | 22 | | using Elsa.Workflows.Options; |
| | | 23 | | using Elsa.Workflows.Runtime.Distributed.Extensions; |
| | | 24 | | using Elsa.Workflows.Runtime.Options; |
| | | 25 | | using Elsa.Workflows.Runtime.Tasks; |
| | | 26 | | using JetBrains.Annotations; |
| | | 27 | | using Microsoft.Extensions.Options; |
| | | 28 | | |
| | | 29 | | // ReSharper disable RedundantAssignment |
| | | 30 | | const bool useReadOnlyMode = false; |
| | | 31 | | const bool useSignalR = false; // Disabled until Elsa Studio sends authenticated requests. |
| | | 32 | | const bool useMultitenancy = false; |
| | | 33 | | const bool disableVariableWrappers = false; |
| | | 34 | | |
| | 7 | 35 | | ObjectConverter.StrictMode = true; |
| | | 36 | | |
| | 7 | 37 | | var builder = WebApplication.CreateBuilder(args); |
| | 7 | 38 | | var services = builder.Services; |
| | 7 | 39 | | var configuration = builder.Configuration; |
| | 7 | 40 | | var identitySection = configuration.GetSection("Identity"); |
| | 7 | 41 | | var identityTokenSection = identitySection.GetSection("Tokens"); |
| | | 42 | | |
| | | 43 | | // Add Elsa services. |
| | 7 | 44 | | services |
| | 7 | 45 | | .AddElsa(elsa => |
| | 7 | 46 | | { |
| | 7 | 47 | | elsa |
| | 7 | 48 | | .AddActivitiesFrom<Program>() |
| | 7 | 49 | | .AddActivityHost<Penguin>() |
| | 7 | 50 | | .AddWorkflowsFrom<Program>() |
| | 7 | 51 | | .UseIdentity(identity => |
| | 7 | 52 | | { |
| | 7 | 53 | | identity.TokenOptions += options => identityTokenSection.Bind(options); |
| | 7 | 54 | | identity.UseConfigurationBasedUserProvider(options => identitySection.Bind(options)); |
| | 7 | 55 | | identity.UseConfigurationBasedApplicationProvider(options => identitySection.Bind(options)); |
| | 7 | 56 | | identity.UseConfigurationBasedRoleProvider(options => identitySection.Bind(options)); |
| | 7 | 57 | | }) |
| | 7 | 58 | | .UseDefaultAuthentication() |
| | 7 | 59 | | .UseWorkflows(workflows => |
| | 7 | 60 | | { |
| | 7 | 61 | | workflows.UseCommitStrategies(strategies => |
| | 7 | 62 | | { |
| | 7 | 63 | | strategies.AddStandardStrategies(); |
| | 7 | 64 | | strategies.Add("Every 10 seconds", new PeriodicWorkflowStrategy(TimeSpan.FromSeconds(10))); |
| | 14 | 65 | | }); |
| | 7 | 66 | | }) |
| | 7 | 67 | | .UseFlowchart(flowchart => flowchart.UseTokenBasedExecution()) |
| | 7 | 68 | | .UseWorkflowManagement(management => |
| | 7 | 69 | | { |
| | 14 | 70 | | management.UseEntityFrameworkCore(ef => ef.UseSqlite()); |
| | 7 | 71 | | management.SetDefaultLogPersistenceMode(LogPersistenceMode.Inherit); |
| | 7 | 72 | | management.UseCache(); |
| | 7 | 73 | | management.UseReadOnlyMode(useReadOnlyMode); |
| | 7 | 74 | | }) |
| | 7 | 75 | | .UseWorkflowRuntime(runtime => |
| | 7 | 76 | | { |
| | 14 | 77 | | runtime.UseEntityFrameworkCore(ef => ef.UseSqlite()); |
| | 7 | 78 | | runtime.UseCache(); |
| | 7 | 79 | | runtime.UseDistributedRuntime(); |
| | 7 | 80 | | }) |
| | 7 | 81 | | .UseWorkflowsApi() |
| | 7 | 82 | | .UseFluentStorageProvider() |
| | 7 | 83 | | .UseElsaScriptBlobStorage() |
| | 7 | 84 | | .UseScheduling() |
| | 7 | 85 | | .UseCSharp(options => |
| | 7 | 86 | | { |
| | 7 | 87 | | options.DisableWrappers = disableVariableWrappers; |
| | 7 | 88 | | options.AppendScript("string Greet(string name) => $\"Hello {name}!\";"); |
| | 7 | 89 | | options.AppendScript("string SayHelloWorld() => Greet(\"World\");"); |
| | 7 | 90 | | }) |
| | 7 | 91 | | .UseJavaScript(options => |
| | 7 | 92 | | { |
| | 7 | 93 | | options.AllowClrAccess = true; |
| | 7 | 94 | | options.ConfigureEngine(engine => |
| | 7 | 95 | | { |
| | 23 | 96 | | engine.Execute("function greet(name) { return `Hello ${name}!`; }"); |
| | 23 | 97 | | engine.Execute("function sayHelloWorld() { return greet('World'); }"); |
| | 30 | 98 | | }); |
| | 7 | 99 | | }) |
| | 7 | 100 | | .UsePython(python => |
| | 7 | 101 | | { |
| | 7 | 102 | | python.PythonOptions += options => |
| | 7 | 103 | | { |
| | 7 | 104 | | // Make sure to configure the path to the python DLL. E.g. /opt/homebrew/Cellar/python@3.11/3.11.6_1 |
| | 7 | 105 | | // alternatively, you can set the PYTHONNET_PYDLL environment variable. |
| | 7 | 106 | | configuration.GetSection("Scripting:Python").Bind(options); |
| | 7 | 107 | | |
| | 7 | 108 | | options.AddScript(sb => |
| | 7 | 109 | | { |
| | 7 | 110 | | sb.AppendLine("def greet():"); |
| | 7 | 111 | | sb.AppendLine(" return \"Hello, welcome to Python!\""); |
| | 14 | 112 | | }); |
| | 14 | 113 | | }; |
| | 7 | 114 | | }) |
| | 14 | 115 | | .UseLiquid(liquid => liquid.FluidOptions = options => options.Encoder = HtmlEncoder.Default) |
| | 7 | 116 | | .UseHttp(http => |
| | 7 | 117 | | { |
| | 14 | 118 | | http.ConfigureHttpOptions = options => configuration.GetSection("Http").Bind(options); |
| | 7 | 119 | | http.UseCache(); |
| | 14 | 120 | | }); |
| | 7 | 121 | | ConfigureForTest?.Invoke(elsa); |
| | 14 | 122 | | }); |
| | | 123 | | |
| | | 124 | | // Obfuscate HTTP request headers. |
| | 7 | 125 | | services.AddActivityStateFilter<HttpRequestAuthenticationHeaderFilter>(); |
| | | 126 | | |
| | | 127 | | // Optionally configure recurring tasks using alternative schedules. |
| | 7 | 128 | | services.Configure<RecurringTaskOptions>(options => |
| | 7 | 129 | | { |
| | 7 | 130 | | options.Schedule.ConfigureTask<TriggerBookmarkQueueRecurringTask>(TimeSpan.FromSeconds(300)); |
| | 7 | 131 | | options.Schedule.ConfigureTask<PurgeBookmarkQueueRecurringTask>(TimeSpan.FromSeconds(300)); |
| | 7 | 132 | | options.Schedule.ConfigureTask<RestartInterruptedWorkflowsTask>(TimeSpan.FromSeconds(15)); |
| | 14 | 133 | | }); |
| | | 134 | | |
| | 21 | 135 | | services.Configure<RuntimeOptions>(options => { options.InactivityThreshold = TimeSpan.FromSeconds(15); }); |
| | 7 | 136 | | services.Configure<BookmarkQueuePurgeOptions>(options => options.Ttl = TimeSpan.FromSeconds(3600)); |
| | 14 | 137 | | services.Configure<CachingOptions>(options => options.CacheDuration = TimeSpan.FromDays(1)); |
| | 8 | 138 | | services.Configure<IncidentOptions>(options => options.DefaultIncidentStrategy = typeof(ContinueWithIncidentsStrategy)); |
| | 7 | 139 | | services.AddHealthChecks(); |
| | 7 | 140 | | services.AddControllers(); |
| | 21 | 141 | | services.AddCors(cors => cors.AddDefaultPolicy(policy => policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().WithE |
| | | 142 | | |
| | | 143 | | // Build the web application. |
| | 7 | 144 | | var app = builder.Build(); |
| | | 145 | | |
| | | 146 | | |
| | | 147 | | // Configure the pipeline. |
| | 7 | 148 | | if (app.Environment.IsDevelopment()) |
| | 7 | 149 | | app.UseDeveloperExceptionPage(); |
| | | 150 | | |
| | | 151 | | // CORS. |
| | 7 | 152 | | app.UseCors(); |
| | | 153 | | |
| | | 154 | | // Health checks. |
| | 7 | 155 | | app.MapHealthChecks("/"); |
| | | 156 | | |
| | | 157 | | // Routing used for SignalR. |
| | 7 | 158 | | app.UseRouting(); |
| | | 159 | | |
| | | 160 | | // Security. |
| | 7 | 161 | | app.UseAuthentication(); |
| | 7 | 162 | | app.UseAuthorization(); |
| | | 163 | | |
| | | 164 | | // Multitenancy. |
| | | 165 | | if (useMultitenancy) |
| | | 166 | | app.UseTenants(); |
| | | 167 | | |
| | | 168 | | // Elsa API endpoints for designer. |
| | 7 | 169 | | var routePrefix = app.Services.GetRequiredService<IOptions<ApiEndpointOptions>>().Value.RoutePrefix; |
| | 7 | 170 | | app.UseWorkflowsApi(routePrefix); |
| | | 171 | | |
| | | 172 | | // Captures unhandled exceptions and returns a JSON response. |
| | 7 | 173 | | app.UseJsonSerializationErrorHandler(); |
| | | 174 | | |
| | | 175 | | // Elsa HTTP Endpoint activities. |
| | 7 | 176 | | app.UseWorkflows(); |
| | | 177 | | |
| | 7 | 178 | | app.MapControllers(); |
| | | 179 | | |
| | | 180 | | // Swagger API documentation. |
| | 7 | 181 | | if (app.Environment.IsDevelopment()) |
| | | 182 | | { |
| | 7 | 183 | | app.UseSwaggerUI(); |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | // SignalR. |
| | | 187 | | if (useSignalR) |
| | | 188 | | { |
| | | 189 | | app.UseWorkflowsSignalRHubs(); |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | // Run. |
| | 7 | 193 | | await app.RunAsync(); |
| | | 194 | | |
| | | 195 | | /// <summary> |
| | | 196 | | /// The main entry point for the application made public for end to end testing. |
| | | 197 | | /// </summary> |
| | | 198 | | [UsedImplicitly] |
| | | 199 | | public partial class Program |
| | | 200 | | { |
| | | 201 | | /// <summary> |
| | | 202 | | /// Set by the test runner to configure the module for testing. |
| | | 203 | | /// </summary> |
| | 15 | 204 | | public static Action<IModule>? ConfigureForTest { get; set; } |
| | | 205 | | } |