< Summary

Information
Class: Program
Assembly: Elsa.Server.Web
File(s): /home/runner/work/elsa-core/elsa-core/src/apps/Elsa.Server.Web/Program.cs
Line coverage
100%
Covered lines: 128
Uncovered lines: 0
Coverable lines: 128
Total lines: 217
Line coverage: 100%
Branch coverage
96%
Covered branches: 27
Total branches: 28
Branch coverage: 96.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
<Main>$()96.42%2828100%
get_ConfigureForTest()100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/apps/Elsa.Server.Web/Program.cs

#LineLine coverage
 1using System.Text.Encodings.Web;
 2using Elsa.Caching.Options;
 3using Elsa.Common.RecurringTasks;
 4using Elsa.Expressions.Helpers;
 5using Elsa.Extensions;
 6using Elsa.Features.Services;
 7using Elsa.Identity.Multitenancy;
 8using Elsa.Persistence.EFCore.Extensions;
 9using Elsa.Persistence.EFCore.Modules.Management;
 10using Elsa.Persistence.EFCore.Modules.Runtime;
 11using Elsa.Server.Web.Activities;
 12using Elsa.Server.Web.ActivityHosts;
 13using Elsa.Server.Web.Filters;
 14using Elsa.Tenants;
 15using Elsa.Tenants.AspNetCore;
 16using Elsa.Tenants.Extensions;
 17using Elsa.WorkflowProviders.BlobStorage.ElsaScript.Extensions;
 18using Elsa.Workflows;
 19using Elsa.Workflows.Activities.Flowchart.Extensions;
 20using Elsa.Workflows.Api;
 21using Elsa.Workflows.CommitStates.Strategies;
 22using Elsa.Workflows.IncidentStrategies;
 23using Elsa.Workflows.LogPersistence;
 24using Elsa.Workflows.Options;
 25using Elsa.Workflows.Runtime.Distributed.Extensions;
 26using Elsa.Workflows.Runtime.Options;
 27using Elsa.Workflows.Runtime.Tasks;
 28using JetBrains.Annotations;
 29using Microsoft.Extensions.Options;
 30
 31// ReSharper disable RedundantAssignment
 32const bool useReadOnlyMode = false;
 33const bool useSignalR = false; // Disabled until Elsa Studio sends authenticated requests.
 34const bool useMultitenancy = true;
 35const bool disableVariableWrappers = false;
 36
 337ObjectConverter.StrictMode = true;
 38
 339var builder = WebApplication.CreateBuilder(args);
 340var services = builder.Services;
 341var configuration = builder.Configuration;
 342var identitySection = configuration.GetSection("Identity");
 343var identityTokenSection = identitySection.GetSection("Tokens");
 44
 345services
 346    .AddElsa(elsa =>
 347    {
 348        elsa
 349            .AddActivitiesFrom<Program>()
 350            .AddActivityHost<Penguin>()
 351            .AddWorkflowsFrom<Program>()
 352            .UseIdentity(identity =>
 353            {
 354                identity.TokenOptions += options => identityTokenSection.Bind(options);
 355                identity.UseConfigurationBasedUserProvider(options => identitySection.Bind(options));
 356                identity.UseConfigurationBasedApplicationProvider(options => identitySection.Bind(options));
 357                identity.UseConfigurationBasedRoleProvider(options => identitySection.Bind(options));
 358            })
 359            .UseDefaultAuthentication()
 360            .UseWorkflows(workflows =>
 361            {
 362                workflows.UseCommitStrategies(strategies =>
 363                {
 364                    strategies.AddStandardStrategies();
 365                    strategies.Add("Every 10 seconds", new PeriodicWorkflowStrategy(TimeSpan.FromSeconds(10)));
 666                });
 367            })
 368            .UseFlowchart(flowchart => flowchart.UseTokenBasedExecution())
 369            .UseWorkflowManagement(management =>
 370            {
 671                management.UseEntityFrameworkCore(ef => ef.UseSqlite());
 372                management.SetDefaultLogPersistenceMode(LogPersistenceMode.Inherit);
 373                management.UseCache();
 374                management.UseReadOnlyMode(useReadOnlyMode);
 375            })
 376            .UseWorkflowRuntime(runtime =>
 377            {
 678                runtime.UseEntityFrameworkCore(ef => ef.UseSqlite());
 379                runtime.UseCache();
 380                runtime.UseDistributedRuntime();
 381            })
 382            .UseWorkflowsApi()
 383            .UseFluentStorageProvider()
 384            .UseElsaScriptBlobStorage()
 385            .UseScheduling()
 386            .UseCSharp(options =>
 387            {
 388                options.DisableWrappers = disableVariableWrappers;
 389                options.AppendScript("string Greet(string name) => $\"Hello {name}!\";");
 390                options.AppendScript("string SayHelloWorld() => Greet(\"World\");");
 391            })
 392            .UseJavaScript(options =>
 393            {
 394                options.AllowClrAccess = true;
 395                options.ConfigureEngine(engine =>
 396                {
 1997                    engine.Execute("function greet(name) { return `Hello ${name}!`; }");
 1998                    engine.Execute("function sayHelloWorld() { return greet('World'); }");
 2299                });
 3100            })
 3101            .UsePython(python =>
 3102            {
 3103                python.PythonOptions += options =>
 3104                {
 3105                    // Make sure to configure the path to the python DLL. E.g. /opt/homebrew/Cellar/python@3.11/3.11.6_1
 3106                    // alternatively, you can set the PYTHONNET_PYDLL environment variable.
 3107                    configuration.GetSection("Scripting:Python").Bind(options);
 3108
 3109                    options.AddScript(sb =>
 3110                    {
 3111                        sb.AppendLine("def greet():");
 3112                        sb.AppendLine("    return \"Hello, welcome to Python!\"");
 6113                    });
 6114                };
 3115            })
 6116            .UseLiquid(liquid => liquid.FluidOptions = options => options.Encoder = HtmlEncoder.Default)
 3117            .UseHttp(http =>
 3118            {
 6119                http.ConfigureHttpOptions = options => configuration.GetSection("Http").Bind(options);
 3120                http.UseCache();
 6121            });
 3122
 3123        if(useMultitenancy)
 3124        {
 3125            elsa.UseTenants(tenants =>
 3126            {
 4127                tenants.UseConfigurationBasedTenantsProvider(options => configuration.GetSection("Multitenancy").Bind(op
 4128                tenants.ConfigureMultitenancy(options => options.TenantResolverPipelineBuilder = new TenantResolverPipel
 4129                    .Append<CurrentUserTenantResolver>());
 6130            });
 3131        }
 3132
 3133        ConfigureForTest?.Invoke(elsa);
 6134    });
 135
 136// Obfuscate HTTP request headers.
 3137services.AddActivityStateFilter<HttpRequestAuthenticationHeaderFilter>();
 138
 139// Optionally configure recurring tasks using alternative schedules.
 3140services.Configure<RecurringTaskOptions>(options =>
 3141{
 3142    options.Schedule.ConfigureTask<TriggerBookmarkQueueRecurringTask>(TimeSpan.FromSeconds(300));
 3143    options.Schedule.ConfigureTask<PurgeBookmarkQueueRecurringTask>(TimeSpan.FromSeconds(300));
 3144    options.Schedule.ConfigureTask<RestartInterruptedWorkflowsTask>(TimeSpan.FromSeconds(15));
 6145});
 146
 9147services.Configure<RuntimeOptions>(options => { options.InactivityThreshold = TimeSpan.FromSeconds(15); });
 3148services.Configure<BookmarkQueuePurgeOptions>(options => options.Ttl = TimeSpan.FromSeconds(3600));
 6149services.Configure<CachingOptions>(options => options.CacheDuration = TimeSpan.FromDays(1));
 3150services.Configure<IncidentOptions>(options => options.DefaultIncidentStrategy = typeof(ContinueWithIncidentsStrategy));
 3151services.AddHealthChecks();
 3152services.AddControllers();
 9153services.AddCors(cors => cors.AddDefaultPolicy(policy => policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().WithE
 154
 155// Build the web application.
 3156var app = builder.Build();
 157
 158
 159// Configure the pipeline.
 3160if (app.Environment.IsDevelopment())
 3161    app.UseDeveloperExceptionPage();
 162
 163// CORS.
 3164app.UseCors();
 165
 166// Health checks.
 3167app.MapHealthChecks("/");
 168
 169// Routing used for SignalR.
 3170app.UseRouting();
 171
 172// Security.
 3173app.UseAuthentication();
 3174app.UseAuthorization();
 175
 176// Multitenancy.
 177if (useMultitenancy)
 3178    app.UseTenants();
 179
 180// Elsa API endpoints for designer.
 3181var routePrefix = app.Services.GetRequiredService<IOptions<ApiEndpointOptions>>().Value.RoutePrefix;
 3182app.UseWorkflowsApi(routePrefix);
 183
 184// Captures unhandled exceptions and returns a JSON response.
 3185app.UseJsonSerializationErrorHandler();
 186
 187// Elsa HTTP Endpoint activities.
 3188app.UseWorkflows();
 189
 3190app.MapControllers();
 191
 192// Swagger API documentation.
 3193if (app.Environment.IsDevelopment())
 194{
 3195    app.UseSwaggerUI();
 196}
 197
 198// SignalR.
 199if (useSignalR)
 200{
 201    app.UseWorkflowsSignalRHubs();
 202}
 203
 204// Run.
 3205await app.RunAsync();
 206
 207/// <summary>
 208/// The main entry point for the application made public for end to end testing.
 209/// </summary>
 210[UsedImplicitly]
 211public partial class Program
 212{
 213    /// <summary>
 214    /// Set by the test runner to configure the module for testing.
 215    /// </summary>
 7216    public static Action<IModule>? ConfigureForTest { get; set; }
 217}

Methods/Properties

<Main>$()
get_ConfigureForTest()