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