< 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: 218
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
 45// Add Elsa services.
 346services
 347    .AddElsa(elsa =>
 348    {
 349        elsa
 350            .AddActivitiesFrom<Program>()
 351            .AddActivityHost<Penguin>()
 352            .AddWorkflowsFrom<Program>()
 353            .UseIdentity(identity =>
 354            {
 355                identity.TokenOptions += options => identityTokenSection.Bind(options);
 356                identity.UseConfigurationBasedUserProvider(options => identitySection.Bind(options));
 357                identity.UseConfigurationBasedApplicationProvider(options => identitySection.Bind(options));
 358                identity.UseConfigurationBasedRoleProvider(options => identitySection.Bind(options));
 359            })
 360            .UseDefaultAuthentication()
 361            .UseWorkflows(workflows =>
 362            {
 363                workflows.UseCommitStrategies(strategies =>
 364                {
 365                    strategies.AddStandardStrategies();
 366                    strategies.Add("Every 10 seconds", new PeriodicWorkflowStrategy(TimeSpan.FromSeconds(10)));
 667                });
 368            })
 369            .UseFlowchart(flowchart => flowchart.UseTokenBasedExecution())
 370            .UseWorkflowManagement(management =>
 371            {
 672                management.UseEntityFrameworkCore(ef => ef.UseSqlite());
 373                management.SetDefaultLogPersistenceMode(LogPersistenceMode.Inherit);
 374                management.UseCache();
 375                management.UseReadOnlyMode(useReadOnlyMode);
 376            })
 377            .UseWorkflowRuntime(runtime =>
 378            {
 679                runtime.UseEntityFrameworkCore(ef => ef.UseSqlite());
 380                runtime.UseCache();
 381                runtime.UseDistributedRuntime();
 382            })
 383            .UseWorkflowsApi()
 384            .UseFluentStorageProvider()
 385            .UseElsaScriptBlobStorage()
 386            .UseScheduling()
 387            .UseCSharp(options =>
 388            {
 389                options.DisableWrappers = disableVariableWrappers;
 390                options.AppendScript("string Greet(string name) => $\"Hello {name}!\";");
 391                options.AppendScript("string SayHelloWorld() => Greet(\"World\");");
 392            })
 393            .UseJavaScript(options =>
 394            {
 395                options.AllowClrAccess = true;
 396                options.ConfigureEngine(engine =>
 397                {
 1998                    engine.Execute("function greet(name) { return `Hello ${name}!`; }");
 1999                    engine.Execute("function sayHelloWorld() { return greet('World'); }");
 22100                });
 3101            })
 3102            .UsePython(python =>
 3103            {
 3104                python.PythonOptions += options =>
 3105                {
 3106                    // Make sure to configure the path to the python DLL. E.g. /opt/homebrew/Cellar/python@3.11/3.11.6_1
 3107                    // alternatively, you can set the PYTHONNET_PYDLL environment variable.
 3108                    configuration.GetSection("Scripting:Python").Bind(options);
 3109
 3110                    options.AddScript(sb =>
 3111                    {
 3112                        sb.AppendLine("def greet():");
 3113                        sb.AppendLine("    return \"Hello, welcome to Python!\"");
 6114                    });
 6115                };
 3116            })
 6117            .UseLiquid(liquid => liquid.FluidOptions = options => options.Encoder = HtmlEncoder.Default)
 3118            .UseHttp(http =>
 3119            {
 6120                http.ConfigureHttpOptions = options => configuration.GetSection("Http").Bind(options);
 3121                http.UseCache();
 6122            });
 3123
 3124        if(useMultitenancy)
 3125        {
 3126            elsa.UseTenants(tenants =>
 3127            {
 4128                tenants.UseConfigurationBasedTenantsProvider(options => configuration.GetSection("Multitenancy").Bind(op
 4129                tenants.ConfigureMultitenancy(options => options.TenantResolverPipelineBuilder = new TenantResolverPipel
 4130                    .Append<CurrentUserTenantResolver>());
 6131            });
 3132        }
 3133
 3134        ConfigureForTest?.Invoke(elsa);
 6135    });
 136
 137// Obfuscate HTTP request headers.
 3138services.AddActivityStateFilter<HttpRequestAuthenticationHeaderFilter>();
 139
 140// Optionally configure recurring tasks using alternative schedules.
 3141services.Configure<RecurringTaskOptions>(options =>
 3142{
 3143    options.Schedule.ConfigureTask<TriggerBookmarkQueueRecurringTask>(TimeSpan.FromSeconds(300));
 3144    options.Schedule.ConfigureTask<PurgeBookmarkQueueRecurringTask>(TimeSpan.FromSeconds(300));
 3145    options.Schedule.ConfigureTask<RestartInterruptedWorkflowsTask>(TimeSpan.FromSeconds(15));
 6146});
 147
 9148services.Configure<RuntimeOptions>(options => { options.InactivityThreshold = TimeSpan.FromSeconds(15); });
 3149services.Configure<BookmarkQueuePurgeOptions>(options => options.Ttl = TimeSpan.FromSeconds(3600));
 6150services.Configure<CachingOptions>(options => options.CacheDuration = TimeSpan.FromDays(1));
 3151services.Configure<IncidentOptions>(options => options.DefaultIncidentStrategy = typeof(ContinueWithIncidentsStrategy));
 3152services.AddHealthChecks();
 3153services.AddControllers();
 9154services.AddCors(cors => cors.AddDefaultPolicy(policy => policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().WithE
 155
 156// Build the web application.
 3157var app = builder.Build();
 158
 159
 160// Configure the pipeline.
 3161if (app.Environment.IsDevelopment())
 3162    app.UseDeveloperExceptionPage();
 163
 164// CORS.
 3165app.UseCors();
 166
 167// Health checks.
 3168app.MapHealthChecks("/");
 169
 170// Routing used for SignalR.
 3171app.UseRouting();
 172
 173// Security.
 3174app.UseAuthentication();
 3175app.UseAuthorization();
 176
 177// Multitenancy.
 178if (useMultitenancy)
 3179    app.UseTenants();
 180
 181// Elsa API endpoints for designer.
 3182var routePrefix = app.Services.GetRequiredService<IOptions<ApiEndpointOptions>>().Value.RoutePrefix;
 3183app.UseWorkflowsApi(routePrefix);
 184
 185// Captures unhandled exceptions and returns a JSON response.
 3186app.UseJsonSerializationErrorHandler();
 187
 188// Elsa HTTP Endpoint activities.
 3189app.UseWorkflows();
 190
 3191app.MapControllers();
 192
 193// Swagger API documentation.
 3194if (app.Environment.IsDevelopment())
 195{
 3196    app.UseSwaggerUI();
 197}
 198
 199// SignalR.
 200if (useSignalR)
 201{
 202    app.UseWorkflowsSignalRHubs();
 203}
 204
 205// Run.
 3206await app.RunAsync();
 207
 208/// <summary>
 209/// The main entry point for the application made public for end to end testing.
 210/// </summary>
 211[UsedImplicitly]
 212public partial class Program
 213{
 214    /// <summary>
 215    /// Set by the test runner to configure the module for testing.
 216    /// </summary>
 7217    public static Action<IModule>? ConfigureForTest { get; set; }
 218}

Methods/Properties

<Main>$()
get_ConfigureForTest()